I have two collections. The first one's documents have the following structure:
{
_id: "some ObjectId"
data: [ 1, 2 ,3 ]
}
The second one's documents have the following structure:
{
_id: "some object id",
value: 1,
name: "first",
}
I want to get document from first collection and add to each item of its data
array field name
from the second collection. Result I want to get looks like
{
_id: "some ObjectId"
data: [
{ id: 1, name: "first"},
{ id: 2, name: "first"},
{ id: 3, name: "first"}
]
}
where "first", "second" and "third" are values of field name
from second collection's items. As I know I should use aggregate and lookup but I don't know how to do it prperly. How do I get this result?
UPD. This question is not duplicate. I know how to use $lookup
but I don't know how to use it with array items!