Sails.js - 查询数组大小

时间:2014-04-17 20:31:42

标签: node.js mongodb sails.js waterline

// Item.js
schema: true,
attributes: {
    testArray: {
        type: 'array',
        required: true,
        array: true
    }
}

我想找到testArray属性具有特定长度的所有项目。

我尝试使用下面的代码,但它不起作用。

Item.find({testArray: {length: 2}}).exec(function (err, items) {
    console.log(items);
});

我也试过了minLengthmaxLengthsize,但仍然没有结果。
有没有办法做到这一点?

我通过Sails.js / Waterline使用 MongoDB

1 个答案:

答案 0 :(得分:2)

实际上这是在文档中

Model.where({ property: { '>': 100 }})

http://sailsjs.org/#!documentation/models

另一种解决方案:

var query = { testArray: { $size: { $gt:2 } } };

Item.native(function(err, collection) {
    collection.find(query).toArray(function(err, items) {
        //items contains objects where testArray.length is greater than 2
    });
});

根据您的mongodb版本,您可能会遇到问题,然后阅读http://www.mkyong.com/mongodb/mongodb-find-all-documents-where-an-array-list-size-is-greater-than-n/

相关问题