node.js - Group by date in mongoose -


this appointment collection

{ _id: objectid("518ee0bc9be1909012000002"), date: isodate("2013-05-13t22:00:00z"), patient:"john" }  { _id: objectid("518ee0bc9be1909012000002"), date: isodate("2013-05-13t22:00:00z"), patient:"alex" }  { _id: objectid("518ee0bc9be1909012000002"), date: isodate("2013-05-13t22:00:00z"), patient:"sara" } 

how can resualt this

{date: isodate("2013-05-13t22:00:00z"), patients:["john","alex","sara"] } 

i try this

appointments.aggregate([ { $group: { _id: '$date' } } ], function(err, doc) { return console.log(json.stringify(doc)); }); 

any please

use $push aggregation operator assemble patients array in $group:

appointments.aggregate([     {$group: {_id: '$date', patients: {$push: '$patient'}}},     {$project: {date: '$_id', patients: 1, _id: 0}} ], ...) 

to include patient ids in follow-up question:

appointments.aggregate([     {$group: {_id: '$date', patients: {$push: {         patient: '$patient'         _id: '$_id'     }}}},     {$project: {date: '$_id', patients: 1, _id: 0}} ], ...) 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -