I have three documents:
{ "id_user": "t57092501745ad6285ac58c22", "name": "Day #1", "date": { "$date": "2016-04-21T20:50:00.190Z" }, "text": "My text" } { "id_user": "t57092501745ad6285ac58c22", "name": "Day #2", "date": { "$date": "2016-04-22T20:50:00.190Z" }, "text": "My text" } { "id_user": "t57092501745ad6285ac58c22", "name": "Day #3", "date": { "$date": "2016-04-22T20:51:00.190Z" }, "text": "My text" }
and I need to group them throughout the day, so I:
{ "$match": { "id_user": "t57092501745ad6285ac58c22" } }, { "$sort": { "date": -1 } }, { "$group": { "_id": { $dayOfYear: "$date" }, "data": { "$push": { "id_user": "$id_user", "name": "$name", "date": "$date", "text": "$text" }, }, } }
and the result:
{ { _id: 113, data: [{ "id_user": "t57092501745ad6285ac58c22", name: "Day #1", date: "2016-04-22T20:51:00.190Z", text: "My text" }] }, { _id: 114, data: [{ "id_user": "t57092501745ad6285ac58c22", name: "Day #3", date: "2016-04-23T20:51:00.190Z", text: "My text" }, { "id_user": "t57092501745ad6285ac58c22", name: "Day #2", date: "2016-04-23T20:50:00.190Z", text: "My text" }] } }
and thatβs fine, but the order is not what I need:
{ Day
if I change sort to { "date": 1 } , I can invert the order of the two groups, as follows:
{ Day
but I donβt know how to also change the order inside the submatrix to get the correct value:
{ Day
What is the right way?