简体   繁体   English

如何在猫鼬中不显示具有特定条件的项目

[英]how not to show items with specific condition in mongoose

My code is as shown below: 我的代码如下所示:

foodtruck.js foodtruck.js

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var Items = require('./items.js');


var FoodTruckSchema = new Schema({
    foodtruck_name:String,
    foodtruck_location:String,
    foodtruck_rating:{type:Number,default:5},
    foodtruck_total_votes:{type:Number,default:0},
    foodtruck_tag:String,
    foodtruck_open_status:{type:Number,default:1},   //0 open 1 closed
    foodtruck_starting_timing:String,
    foodtruck_closing_timing:String,
    foodtruck_cusine:[String],
    foodtruck_img:String,
    foodtruck_logo:String,
    item_list: [ {type : mongoose.Schema.ObjectId, ref : 'items'}]
},{ versionKey: false });



module.exports = mongoose.model('foodtruck',FoodTruckSchema);

items.js items.js

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var ItemSchema = new Schema({
    no_of_times_ordered:Number,
    item_name:String,
    item_tag:String,
    item_category:String,
    item_description:String,
    item_illustrations:[String],
    item_stock:Number,   //0 available 1 last 5 items 2 not available
    item_quantity_ordered:{type:Number,default:0},
    item_discount_price:Number,
    item_price:Number,
    item_img:String,
    no_of_likes:{type:Number,default:0}
},{ versionKey: false });

module.exports = mongoose.model('items',ItemSchema);

My query is as show below: 我的查询如下所示:

var foodtrucklist = function(req, res) {
    foodtr.find().populate('item_list').exec(function(err, foodtrucks) {
        foodtrucks.forEach(function(ftr) {
            var start_time = ftr.foodtruck_starting_timing;
            var end_time = ftr.foodtruck_closing_timing;
            var foodtruck_open_status = ftr.foodtruck_open_status;
            // var shut_down = ftr.foodtruck_shutdown;
            if ((start_time && end_time) &&
                (start_time.trim() != '' &&
                    end_time.trim() != '')) {
                if (inTime(start_time, end_time) &&
                    foodtruck_open_status ==0 ) {
                    ftr.foodtruck_open_status = 0;
                    ftr.save();
                } else {
                    ftr.foodtruck_open_status = 1;
                    ftr.save();
                }
            }
        })

        res.json({
            status: '200',
            message: 'foodtrucklist',
            data: foodtrucks
        });
    });
};

now what I want to achieve is, I don't want to show items which have **item_stock = -1 **. 现在我要实现的是,我不想显示具有** item_stock = -1 **的项目。 How can I achieve in populate query? 如何在填充查询中实现?

you can use populate options inside your populate ( match for query condition). 您可以使用populate options你里面populatematch查询条件)。

Try this: 尝试这个:

foodtr
.find()
.populate({ 
    path :'item_list', 
    match : { 
        item_stock : { $ne : -1} 
}).
exec(function(err, foodtrucks) {
    ...
});

For more information on populate options, read Mongoose populate query condtions and options documentation , look for query condition and options 有关填充选项的更多信息,请阅读Mongoose填充查询条件和选项文档 ,查找查询条件和选项

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM