简体   繁体   English

当按数值过滤时,猫鼬查询不适用于where()

[英]Mongoose query doesn't work with where() when filtering by numeric values

So I am having trouble with queries using mongoose when using the where clause. 所以我在使用where子句时遇到了使用猫鼬的查询的麻烦。 They work when I filter on strings, but not with numbers. 当我过滤字符串时,它们起作用,但不过滤数字。

Here is my model/schema: 这是我的模型/模式:

// Schema
var Wheel = new  mongoose.Schema({
    name: String,
    method: String,
    size: Number,
    width: Number,
    weight: Number
});


// Model
var WheelModel = mongoose.model('Wheel', Wheel);

This query works: 该查询有效:

var query = WheelModel.find();

query.where('method').equals('Cast/Spun');
query.limit(10);

query.exec(function (err, wheels) {
    // wheel objects are returned
});

This is an example of a wheel object inside the array 'wheels' 这是数组'wheels'中的wheel对象的示例

{
"_id": "523de98b263add11a8d4fc4a",
"name": "AME Circlar RS",
"weight": 18.1,
"width": 7,
"method": "Cast/Spun",
"size": 15
}

This query below returns [], and does so for if I filter by size, width, and weight, and for any numbers 下面的查询返回[],如果我按尺寸,宽度和重量以及任何数字进行过滤,则返回

var query = WheelModel.find();

query.where('size').equals(15);
query.limit(10);

query.exec(function (err, wheels) {
    if (!err) {
        return res.send(wheels);
    } else {
        return console.log(err);
    }
});

I have also tried 我也尝试过

query.where('size', 15);
query.where('size', '15');
var query = WheelModel.find({ size: 15});

If I go: 如果我走:

query.where('size').ne(15);

Then I get results back, but they will include values where the size is 15. So I suspect I have some type issues, I just don't know what. 然后我得到结果,但是它们将包含大小为15的值。所以我怀疑我遇到了一些类型问题,我只是不知道是什么。 Any help would be apprecicated! 任何帮助将不胜感激!

Try the following schema it should work for you (as per understanding from your comments) with the same query ie query.where('size').equals(15) 尝试以下模式,该模式应该对您使用相同的查询query.where('size').equals(15)根据您对注释的理解) query.where('size').equals(15)query.where('size').equals(15)

var Wheel = new  mongoose.Schema({
    name: String,
    method: String,
    size: { type: Number },
    width: { type: Number},
    weight: { type: Number }
});

For more information, check this Schema Types in API docs. 有关更多信息,请在API文档中检查此架构类型

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

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