【问题标题】:Sails-mongo. Find in array帆蒙戈。在数组中查找
【发布时间】:2013-10-01 07:38:24
【问题描述】:

我正在使用sails.js 和sails-mongo 适配器。假设我有一个模型:

module.exports = {

  attributes: {

        shema: true
    ,   attributes: {
                description: {
                    type: 'TEXT'
                ,   max: 200
            }

            ,   tags: {
                    type: 'ARRAY'
            }
    }

  }

};

如何在标签数组中进行搜索?

【问题讨论】:

    标签: node.js mongodb express sails.js


    【解决方案1】:
    Model.find({
        'tags.title': {
            contains: 'query'
          }
    })
    .done(function (err, response) {
        /**/
    });
    

    【讨论】:

    • 查看代码,它运行的是昂贵的 mongo 的 $regex。看来不存在对 mongo 实际数组操作的支持
    【解决方案2】:
    db.schools.find( { criteria },
                 { atributes: { $elemMatch: { tags: value } } } )
    

    这里有一个很好的例子:http://docs.mongodb.org/manual/reference/operator/projection/elemMatch/

    带水线

    Model.native(function(err, collection) {
        // Execute any query that works with the mongo js driver
        collection.find( { criteria },
                     { atributes: { $elemMatch: { tags: value } } } )
        });
    

    【讨论】:

    • 您的回答显示了如何通过 mongo cli 执行此搜索。问题是问如何通过 waterline.js 实现搜索
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-08
    • 1970-01-01
    • 2013-09-27
    相关资源
    最近更新 更多