0
votes

DB collection is like: "suid" : "8986691b1db3d99b6fbe27cceac2e19c", "auid" : "22e7e5632acb4ac154b9bcb95710ca38", "msg" : "ceshi1", "ctime" : ISODate("2013-05-22T03:52:26.344Z"), "sended" : true, "__v" : 0, "_id" : ObjectId("519c411351db351236000001")

MongoDB shell command is: db.runCommand({"group":{ns:"messages",key:{"auid":true},"initial":{"lastmsgtime":0},condition:{auid:'456'},$reduce:function(doc,prev){senttime=doc.ctime.valueOf();if(senttime>prev.lastmsgtime){prev.suid=doc.suid;prev.lastmsgtime=new Date(senttime);}}, } })

Can mongoose do this job?

1

1 Answers

0
votes

Mongoose doesn't have direct support for group, so you need to access the native driver's group method via your Mongoose model like this:

Model.collection.group({"auid":true}, ...);