【问题标题】:Distinct Query is Sails js不同的查询是 Sails js
【发布时间】:2014-03-05 06:40:39
【问题描述】:

我想从 mongodb 中检索不同的记录。我尝试了以下方法,

        Model.find()
             .distinct('username')
             .where({ age: 21 })
             .exec(function(err, user) { });

但我收到如下错误。

     TypeError: Object [object Object] has no method 'distinct'.

请帮忙。

【问题讨论】:

    标签: node.js mongodb sails.js


    【解决方案1】:

    这些功能并不是您真正想要的,有更好的方法来获取sails 使用的模型中的“好东西”。

    您确实需要 aggregate 方法,作为示例,您的等价物是:

       Model.native(function(err,collection){
           collection.aggregate([
               { "$match": { "age": 21 } },
               { "$group": { "_id": '$username' } }
           ],function(err,docs) {
              // something here
    
           });
       });
    

    要了解更多幕后部分,请查看 this answer 以获取有关聚合框架的更多链接。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-10
      • 2015-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多