I have the following document structure...
{
"id":"documentID"
"sessionId":"sometext"
"msg":"sometext"
"time":"date"
}
- sessionId can exist in many documents
I want to aggregate the documents by sessionId
, the result for each session should contain the set of messages related to the session sorted by time.
Using the MongoDB aggregation framework how can I achieve that?
I have tried to sort first and then group but the messages in each session wasn't sorted for some reason:
{ $sort: { "time": 1 } },
{ "$group" : {
"_id" : "$sessionId",
"msgs" : { "$addToSet" : "$msg" }
} }
any suggestions? your answer is highly appreciated.