4

I need to get the count of occurrence of a certain value in a collection, like this:

[
    {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'},
]

I want the waterline find() parameters that return:

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

So far I'm able to get everything except the count, with this:

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

I've tried with "sum: ['1']" but that only gives me an attribute named "1" with value 0 in each result row.

Diego Pamio
  • 1,377
  • 13
  • 21

1 Answers1

0

Use aggregate method of node.js mongo DB native documentation

Example from my code: 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});
        });
}
1nstinct
  • 1,745
  • 1
  • 26
  • 30