node.js - Aggregate with Sorting a Date not working -


this similar question on stackoverflow reason not work. have spent lot of time without no success.

here collection setup

{  "_id" : objectid("5715acfcf1dbdc7c0ae94379"),  "users":[objectid("570d2308ba5bc6842242e881"),           objectid("570d7e4b369ac0c525e98331")          ], "messages" : [     {         "user" : objectid("570d2308ba5bc6842242e881"),          "message" : numberint(0),          "readind" : "n",          "createdate" : isodate("2016-04-19t03:59:12.587+0000"),          "_id" : objectid("5715ad10f1dbdc7c0ae94396")     },      {         "user" : objectid("570d2308ba5bc6842242e881"),          "message" : numberint(1),          "readind" : "n",          "createdate" : isodate("2016-04-19t04:11:10.541+0000"),          "_id" : objectid("5715afdef36f23a10ad12348")     },      {         "user" : objectid("570d2308ba5bc6842242e881"),          "message" : numberint(2),          "readind" : "n",          "createdate" : isodate("2016-04-19t04:11:11.756+0000"),          "_id" : objectid("5715afdff36f23a10ad12352")     } ] 

}

and here code snipper mongoose

var objid = new objectid("5715acfcf1dbdc7c0ae94379");  chatmodel.aggregate([     { "$match": { "users": {'$in':[objid]}} },     {         "$project": {             "messages":1         }      }     ,{"$sort": {"messages.createdate": -1} } ],function(err,records) {     if (err) {         return res.json({status: false, errcode: 900, errmsg: err});     }      res.json({status:true,resdata:records}) ;  }); 

thanks,

i see 2 issues here.

  1. your match not match data structure. should testing against message.user instead of users:

    { "$match": { "messages.user": {'$in':[objid]}} } 
  2. you testing using objectid match _id field , not user field. should testing against 570d2308ba5bc6842242e881, not 5715acfcf1dbdc7c0ae94379.

if want test against _id match this:

{ "$match": { "_id": {'$in':[objid]}} } 

Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -