开发者

group records on key

I have collection structure as follows

{"pid" : 1 , id" : 2 ,"auther" : "xyz" , "book" : "pqr" , "time" : 1}

{"pid" : 1 ,"id" : 3 ,"auther" : "abc" , "book" : "stu" , "time" : 2}

{"pid" : 1 ,"id" : 2 ,"auther" : "def" , "book" : "vwx" , "time" : 3}

{"pid" : 1 ,"id" : 3 ,"auther" : "hij" , 开发者_运维技巧"book" : "yza" , "time" : 4}

{"pid" : 2 ,"id" : 3 ,"auther" : "hij" , "book" : "yza" , "time" : 4}

{"pid" : 2 ,"id" : 2 ,"auther" : "def" , "book" : "vwx" , "time" : 3}

I want to records sorted on time in descending order with same id records are group together.

means id containing 2 are group together.

currently may query is just like

db.coll.find({"pid" : 1}).sort({"time" : -1})

Is it possible in mongoDB to group record on field

Thanks.


I'm not sure I understand your problem (What did you mean "group"? - please write result you want).

Try this:

.find().sort({pid:1, time: -1})

First - sorting by pid ASC than sorting by time DESC

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜