【问题标题】:Count GroupBy in Waterline with sails-mongo使用 Sails-mongo 在 Waterline 中计数 GroupBy
【发布时间】:2015-09-28 12:04:32
【问题描述】:

我需要获取集合中某个值的出现次数,如下所示:

[
    {author: 'Diego', name: 'This is a great post', date:'03/13/78'},
    {author: 'Raul', name: 'Recipe for success', date:'02/03/99'},
    {author: 'Diego', name: 'Having too much fun', date:'01/01/77'},
    {author: 'Diego', name: 'Another post by me', date:'10/9/99'},
    {author: 'Diego', name: 'Mi first post', date:'01/01/73'},
    {author: 'Mariano', name: 'Mi best post', date:'01/01/95'},
]

我想要返回的水线 find() 参数:

[
    {author: 'Diego', count: 4, date: '01/01/73'},
    {author: 'Raul', count: 1, date: '02/03/09'}
]

到目前为止,我能够得到所有的东西除了,用这个:

Model.find({
    where: {author: {'!': 'Mariano'}},
    groupBy: ['author'],
    min: ['date']
// and count?!?!?!
}).exec(function(err, items) {
    //do something with items.
});

我尝试过使用“sum: ['1']”,但这只会给我一个名为“1”的属性,每个结果行中的值为 0。

【问题讨论】:

    标签: sails.js waterline sails-mongo


    【解决方案1】:

    使用node.js mongo DB native的聚合方法 documentation

    我的代码示例: select count from order where restaurant ID = ... GROUP BY createdAt ISO DATE BY its first 10 chars - days (2015-01-10)

    if(restaurantsFound.length > 0) {
        var result = [],
            Q = require('q'),
            promises = [];
    
        for(var r in restaurantsFound) {
            promises[r] = (function () {
                var deferred = Q.defer();
    
                Order.native(function(err, collection) {
                    if (err) {
                        console.error(err);
                        deferred.reject(err);
                    }
    
                    var ObjectID = require('mongodb').ObjectID;
                    var restaurantId = new ObjectID(restaurantsFound[r]);
                    collection.aggregate([
                        { $match : {
                            restaurant : restaurantId
                        }},
                        { $group : {
                            _id : {"_id":{"$substr":["$createdAt", 0, 10]}},
                            count : { $sum : 1 },
                            restaurant: {$addToSet: "$restaurant"}
                        }}
                    ], function(err, result) {
                        deferred.resolve(result);
                    });
                });
    
                return deferred.promise;
            })();
        }
    
        Q
            .allSettled(promises)
            .then(function(data) {
                var result = [];
                for(var k in data) {
                    result[k] = [];
                    for(var l in data[k].value) {
                        result[k].push({
                            restaurant: data[k].value[l].restaurant[0].toString(),
                            count: data[k].value[l].count,
                            date: data[k].value[l]._id._id
                        });
                    }
    
                }
                return res.ok(result);
            }).catch(function(error) {
                console.error(error.message);
                return res.badRequest({message: error.message});
            });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-12
      • 2015-06-21
      相关资源
      最近更新 更多