2
votes

Hello i have the following query that executed in Robomongo works as expected:

db.av.group(
{
 key: { roomId: 1},
 cond: { dateOfDay: { $gte: new Date('12/01/2014'), $lt: new Date('12/30/2014') } },
 reduce: function( curr, result ) {
            result.total += curr.price;
            result.count++;
         },
 initial: { total : 0,
     count : 0
     },
     finalize: function(result) {
              result.avg = Math.round(result.total / result.count);
          }

  }
)

After implemented that in a express app :

 app.get('/api/checkAv/:checkIn/:checkOut', function(req, res) {
        var checkIn = req.params.checkIn,
            checkOut = req.params.checkOut,
            roomType = req.params.roomType;

    model.Av.group(
    {
     key: { roomId: 1},
     cond: { dateOfDay: { $gte: new Date('12/01/2014'), $lt: new Date('12/30/2014') } },
     reduce: function( curr, result ) {
                result.total += curr.price;
                result.count++;
             },
     initial: { total : 0,
         count : 0
         },
         finalize: function(result) {
                  result.avg = Math.round(result.total / result.count);
              }

    }

    ).exec(function(err, av) {
        if (err)
            res.send(err);

        res.json(av);
    });
});

i get on the console :

TypeError: Object function model(doc, fields, skipId) { if (!(this instanceof model)) return new model(doc, fields, skipId); Model.call(this, doc, fields, skipId); } has no method 'group' at /home/www/domain.com/api/routes/routes.js:58:18 at Layer.handle [as handle_request] (/home/www/domain.com/node_modules/express/lib/router/layer.js:82:5)
at next (/home/www/domain.com/node_modules/express/lib/router/route.js:100:13) at Route.dispatch (/home/www/domain.com/node_modules/express/lib/router/route.js:81:3)
at Layer.handle [as handle_request] (/home/www/domain.com/node_modules/express/lib/router/layer.js:82:5)
at /home/www/brasovapartments.com/node_modules/express/lib/router/index.js:234:24 at param (/home/www/domain.com/node_modules/express/lib/router/index.js:331:14) at param (/home/www/domain.com/node_modules/express/lib/router/index.js:347:14) at param (/home/www/domain.com/node_modules/express/lib/router/index.js:347:14) at Function.proto.process_params (/home/www/domain.com/node_modules/express/lib/router/index.js:391:3)

now reading around i saw some people defined a group property but not sure how and where .. in the Mongoose schema? Can you gave me a hint what's my problem over-here?

1

1 Answers

9
votes

The group command isn't supported by Mongoose, and was also deprecated in MongoDB 3.4 as its functionality is better served by using aggregate instead.

You can do this with aggregate using something like:

model.Av.aggregate([
    {$match: {dateOfDay: {$gte: new Date('12/01/2014'), $lt:new Date('12/30/2014')}}},
    {$group: {
        _id: '$roomId',
        total: {$sum: '$price'},
        count: {$sum: 1},
        avg: {$avg: '$price'}
    }}
], function (err, result) {...});

You can omit the total and count fields if you don't need them as the $avg operator lets you calculate the average price directly.