首页 > mongoose查询出的文档再如何进一步筛选键

mongoose查询出的文档再如何进一步筛选键

1、通过在schema层添加的static方法,查询出了数组文档的集合,如何进一步删除不想要的键值呢
代码:

   //Schema
    var ProductSchema = new mongoose.Schema({
        productId: Number,
        productTitle: String, 
        productCover: {
            url: String,
            description : String
        },
        categroyType: Number,
    })
    //Static
    ProductSchema.statics.getByCategory = function(category,cb){
    return this.where('categroyType').equals(category).select({_id:0,meta:0,__v:0})
    .exec(cb);
    
    然后再model层直接使用static的方法,忽略model的模块依赖~
    
    Product.getByCategory(1,function(err,product){
        if (err){
            console.log(err)
        }
        else{
            console.log(product) //这里查询出的数组形式的文档集合  如何删除掉不需要的键值对呢?
        }
    })

仅提供思路,未验证。

方法1:逐个删除item

把product里每一个item都调用一下remove

return Promise.all(product.map(item=>item.remove()))

方法2:根据id删除

提取product中所有id

return Product.remove({_id:{$in:product.map(item=>item._id)}},{multi:true})

需要进行数据加工

【热门文章】
【热门文章】