I have a groups
collection with documents of the form
{
"_id": "g123"
...,
"invites": [
{
"senderAccountId": "a456",
"recipientAccountId": "a789"
},
...
]
}
I want to be able to list all the invites received by a user.
I thought of using an aggregation pipeline on the groups
collection that filters all the groups to return only those to which the user has been invited to.
db.groups.aggregate([
{
$match: {
"invites.recipientAccountID": "<user-id>"
}
}
])
Lastly I want to project this array of groups to end up with an array of the form
[
{
"senderAccountId": "a...",
"recipientAccountId": "<user-id>",
"groupId": "g...", // Equal to "_id" field of document.
},
...
]
But I’m missing the "project" step in my aggregation pipeline to bring to the top-level the nested senderAccountId
and recipientAccountId
fields. I have seen examples online of projections in MongoDB queries and aggregation pipelines but I couldn’t find examples for projecting the previously matched element of an array field of a document to the top-level.
I’ve thought of using Array Update Operators to reference the matched element but couldn’t get any meaningful progress using this method.
2
Answers
I think what you want is
$replaceRoot
:See how it works on the playground example
There are multiple ways to do this, using a combination of
unwind
andproject
would work as well.Unwind
will create one object for each andproject
let you choose how you want to structure your result with current variables.You can also use nimrod serok’s
$replaceRoot
in place of the$project
oneplayground
nimrod serok’s solution might be a bit better because mine unwind it first and then matches it but I believe mine is more readable