【问题标题】:Query criteria does not work in mongoose query in node.js查询条件在 node.js 中的猫鼬查询中不起作用
【发布时间】:2017-03-01 23:38:27
【问题描述】:

我的数据库架构如下:

var Items = new Schema({
    no_of_times_ordered:Number,
    item_name:String,
    item_tag:String,
    item_category:String,
    item_stock:Number,
    item_price:Number,
    item_img:String,
    item_illustrations:[String],
    item_liked:Boolean,
    no_of_likes:Number
},{ versionKey: false });


var FoodTruckSchema = new Schema({
    foodtruck_name:String,
    foodtruck_location:String,
    foodtruck_rating:Number,
    foodtruck_tag:String,
    foodtruck_timing:String,
    foodtruck_cusine:String,
    foodtruck_img:String,
    foodtruck_logo:String,
    item_list: [Items]
},{ versionKey: false });



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

查询如下:

var popularitems = function(req,res) {
    foodtr.find({ 'item_list.no_of_times_ordered': { $gt: 4000}},function(err,items){
        if (err) res.send(err);
        res.json({
            status : '200',
            message:'popular items list',
            data: items
        });
    });

在这里,我想要 no_of_times_order 超过 3000 次的项目,但查询似乎不起作用,它给了我集合中的每个项目。

【问题讨论】:

  • 查询不应该是foodtr.find({ 'item_list.no_of_times_ordered': { $gt: 3000 } },function(err, items) {吗?
  • 是的,和你说的一样。但它不起作用
  • foodtr 还是 foodtruck

标签: node.js mongodb mongoose mongoose-schema


【解决方案1】:

我认为您的查询是错误的。如果您想要 no_of_times_ordered,您应该在查询中指定它。例如:

var popularitems = function(req,res) {
    foodtr.find({ 'item_list.no_of_times_ordered': { $gt: 4000}},function(err,items){
        if (err) res.send(err);
        res.json({
            status : '200',
            message:'popular items list',
            data: items
        });
    });

希望我的回答对您有所帮助。

【讨论】:

  • 我按照你说的做了,但是列表是空的
  • 我忘记将 item_list 放在项目的 nbr 之前。请考虑编辑后的答案。
  • 我这样做了,但它给了我完整的列表,没有任何查询过程
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-10-10
  • 1970-01-01
  • 2015-01-04
  • 2021-11-23
  • 2020-06-02
  • 2012-02-03
  • 1970-01-01
相关资源
最近更新 更多