Wednesday, 23 September 2015

mongodb distinct


For one filed:

  Activity.find().distinct("deviceId", function (err, activitys) {
     if(err) { return handleError(res, err); }
     return res.json(200, activitys);
   });

you will get:

[123,345,676543]




For distinct multi fileds:

Users.aggregate( { $group: { _id: {name:"$name", age:"$age" }}} , function (err, res) { if (err) return handleError(err); console.log(res); // [ { maxBalance: 98000 } ] });

_id is needed.

the date you will get is:

[
{_id:  {name: "peter", age: "20" } },
{_id:  {name: "pete", age: "21" } },
{_id:  {name: "jake", age: "22" } }]


http://mongoosejs.com/docs/api.html#model_Model.aggregate

No comments:

Post a comment