I have 2 collections: one called places and type_places. a place has associated a type of place (type_places) and this can have associated some objects from this(objects array).
type_places
{
"_id": "5fbc7cc705253c2da482023f",
"type_place": "office",
"objects": [
{
"_id": "5fbc7cc705253c2da48202saw",
"name": "chair"
},
{
"_id": "5fbc7cc705253c2da4820242",
"name": "table"
},
{
"_id": "5fbc7cc705253c2da482025f",
"name": "desktop"
}
]
}
places
{
"_id": "5fbc7cc705253c2da482025f",
"place": "Room 5",
"type_place_id": "5fbc7cc705253c2da482023f", /*"office"*/
"type_place_objects": [
{
"_id": "5fbc7cc705253c2da48202saw", /*chair*/
"quantify": 4
},
{
"_id": "5fbc7cc705253c2da482025f", /*desktop*/
"quantify": 2
}
]
}
then I want that when I query a place, the query shows me the place that I am consulted, what kind of place it is (type_place) and what objects of that type of place it has
output desired:
{
"_id": "5fbc7cc705253c2da482023f",
"place": "Room 5",
"type_place_objects": [
{
"_id": "5fbc7cc705253c2da48202saw",
"name": "chair",
"quantify": 4
},
{
"_id": "5fbc7cc705253c2da482025f",
"name": "desktop",
"quantify": 2
}
]
}
I am trying this but not works:
place.aggregate(
[
{
"$match": {"place":"Room 5"}
},
{
"$lookup": {
"from": "type_place",
"localField": "type_place_id",
"foreignField": "_id",
"as": "type_place_objects"
}
},
{
"$sort": {
"_id": -1
}
},
{
"$project": {
"_id":1,
"place":1,
"type_place_objects": 1
}
}
])
how can do fix it?