我的架构如下:
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_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
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_illustrations:[String],
item_stock:Number, //0 available 1 last 5 items 2 not available
item_quantity_ordered:{type:Number,default:0},
item_price:Number,
item_img:String,
no_of_likes:{type:Number,default:0}
},{ versionKey: false });
module.exports = mongoose.model('items',ItemSchema);
现在,我希望foodtruck有这样的项目列表,其中的所有项目都应该 no_of_times_ordered 大于4000.为此,我提出问题如下:
foodtr.find({
'item_list.no_of_times_ordered': {
$gt: 4000
}
}, function(err, items) {
if (err) res.json({
status: '404',
message: 'there is no data available'
});
res.json({
status: '200',
message: 'popular items list',
data: items
});
});
};
但是在这里,我得到空列表,那么如何根据上述要求提供项目清单?
答案 0 :(得分:0)
您查询错误,您无法使用item_list.no_of_times_ordered
作为参考文档。您需要使用populate
,并且仅针对选择性项目,使用match
query
populate
内的条件。
试试这个:
foodtr.find({}).populate({
path : 'item_list',
match : {no_of_times_ordered : { $gt :4000}}
}).exec(function(err, items) {
...
});