I have the following document schema in Mongo:
{
"_id": "an id",
"title": "my title",
"muted": "true",
"participants": [{ "userid":12345, "more": "data" }, ... ],
"messages": [{ "message", "details " } ... { } ]
}
I'm trying to get an array of objects in this form:
[
{
"_id": "an id",
"meta"
{
"title": "my title",
"muted": "true",
"participants": [12345, /* user ids only */ ],
},
"messages": [{ "message", "details " } ... { } ]
}
]
I've got the aggregation working to generate messages and _id:
[
{ $match:
{
'participants.user_id': userId
}
},
{ $unwind: "$messages" },
{ $match: { 'messages.sent_at': { '$gte': new Date(date) }}},
{ $group: { _id: "$_id", messages: { $addToSet: "$messages" }}}
]
What is the magic I need to get the meta data?