node.js - Mongoose group aggregation don't return the rest of fields -
hello have next code working now:
app.get('/api/checkav/:checkin/:checkout', function(req, res) { var checkin = req.params.checkin, checkout = req.params.checkout, roomtype = req.params.roomtype, startdate = moment(checkin), enddate = moment(checkout), nights = enddate.diff(startdate, 'days'); model.av.aggregate([ {$match: {dateofday: {$gte: new date(checkin), $lt:new date(checkout)} }}, {$group: { _id: '$roomid', total: {$sum: '$price'}, count: {$sum: 1}, avg: {$avg: '$price'} }}, { $match: { count: { $gte: nights } } } ], function (err, result) { if (err) res.send(err); res.json(result); }); });
it homecoming me like:
[ { "_id": "545b4848344044fa3ec9ac74", "total": 180, "count": 2, "avg": 90 } ]
but want homecoming me other field collection don't need/have them in grouping clause eg : available , cost ...
reading mongoose docs understand need utilize $project issue added like:
{$project: {price: "$price"}}
and empty results ... tried adding in $group cause :
price: '$price' no success : "exception: grouping aggregate field 'price' must defined look within object", goggled errors without success how should accomplish this?
you add together them $group
, need utilize 1 of accumulator operators determine how want multiple values per _id
(roomid
) reduced single value.
if think it, adding price: "$price"
makes no sense cost value expect use?
so need like:
price: {$first: '$price'} // utilize first cost roomid (be sure sort)
or
price: {$push: '$price'} // set of prices roomid array
or
price: [$max: '$price'} // utilize maximum of prices roomid
etc...hopefully idea.
node.js mongodb mongoose mongodb-query
No comments:
Post a Comment