I have two collections, namely food_addons and food_tags
A sample food_tag looks like this
"_id": "63295ae7981b4314003ddbd1",
"outlet_id": "63031a61ade51cae66eec7e1",
"tags": [
{
"name": "cheeses",
"_id": "63295ae7981b4314003ddbd2"
},
{
"name": "beverage",
"_id": "632995bdc6ab38a142d85650"
}
],
"createdAt": "2022-09-20T06:17:11.136Z",
"updatedAt": "2022-09-20T11:19:34.770Z",
"__v": 2,
"id": "63295ae7981b4314003ddbd1"
And the Food addon Looks like his
_id": "632addc8d41e7f277469a619",
"outlet_id": "63031a61ade51cae66eec7e1",
"addons": [
{
"name": "Cheese Minor",
"price": 1239,
"description": "Cheese minor",
"is_avaliable": true,
"type": "veg",
"tags": [
"63295ae7981b4314003ddbd2"
],
"_id": "632addc8d41e7f277469a61a"
},
{
"name": "Cheese Minor",
"price": 1239,
"description": "Cheese minor",
"is_avaliable": true,
"type": "veg",
"tags": [
"63295ae7981b4314003ddbd2"
],
"_id": "632addced41e7f277469a620"
},
{
"name": "Cheese Minor",
"price": 1239,
"description": "Cheese minor",
"is_avaliable": true,
"type": "veg",
"tags": [
"63295ae7981b4314003ddbd2"
],
"_id": "632addf954c84b3eb6dfbac8"
},
{
"name": "Cheese Minor",
"price": 1239,
"description": "Cheese minor",
"is_avaliable": true,
"type": "veg",
"tags": [
"63295ae7981b4314003ddbd2"
],
"_id": "632ade56232424f78f7b1647"
},
{
"name": "Cheese Minor",
"price": 1239,
"description": "Cheese minor",
"is_avaliable": true,
"type": "veg",
"tags": [
"63295ae7981b4314003ddbd2"
],
"_id": "632adef57a8def7b558a5701"
},
{
"name": "Cheese Minor",
"price": 1239,
"description": "Cheese minor",
"is_avaliable": true,
"type": "veg",
"tags": [
"63295ae7981b4314003ddbd2"
],
"_id": "632adf1c1574d90a5139a8fd"
}
],
"createdAt": "2022-09-21T09:47:52.097Z",
"updatedAt": "2022-09-21T09:53:32.031Z",
"__v": 5
What I am trying to achieve is to populate the tags field on food_addons with reference to the food_tags collections for a particular outlet.
I wrote this query but seemed not to work.
let data = await FoodAddOnsModel.aggregate([
{
$match: {
outlet_id: mongoose.Types.ObjectId(outlet_id),
},
},
{
$project: {
_id: 1,
outlet_id: 1,
addons: 1,
},
},
{
$lookup: {
from: "food_tags",
localField: "addons.tags",
foreignField: "tags._id",
as: "tagss",
},
},
]);