0

So I am having trouble with queries using mongoose when using the where clause. 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'

{
"_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. Any help would be apprecicated!

4

1 回答 1

1

尝试以下模式,它应该适用于您(根据您的评论的理解),使用相同的查询,即query.where('size').equals(15)

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

有关更多信息,请查看API 文档中的架构类型

于 2013-09-26T06:44:49.237 回答