如何获取没有引用模式的空数组的模式的特定文档

时间:2017-06-07 11:31:38

标签: node.js mongoose mongoose-schema mongoose-populate

我的代码如下所示:

foodtruck.js

var FoodTruckSchema = new Schema({
    foodtruck_name:String,
    item_list: [ {type : mongoose.Schema.ObjectId, ref : 'items'}]
},{ versionKey: false });

items.js

var ItemSchema = new Schema({
    item_name: String,
    item_description: String,
    item_illustrations: [String],
    item_stock: {
        type: Number,
        default: 0
    } ,
    no_of_likes: {
        type: Number,
        default: 0
    }
}, {
    versionKey: false
});

我的查询如下所示:

const searchItems = (req, res) => {
    let item_name = req.query.item_name;
    foodtr.find().populate({
        path: 'item_list',
        match: {
            item_name: item_name,
            item_stock: { $ne: 2 }
        }
    }).exec(function(err, foodtrucks) {
        res.json({
            status: '200',
            message: 'searched items',
            data: foodtrucks
        });
    });
};

我想要的是,我只想要查询 item_name 与引用的item_name匹配的那些foodtrucks,否则,我不想显示那个特定的foodtruck。现在,我还得到那些 item_list 是空数组的食物。如何缓解这种情况?

0 个答案:

没有答案