MongoDB Aggreagation

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",
      },
    },
  ]);

Hi @Samson_Kwaku_Nkrumah,

Could you provide the following information:

  1. MongoDB version in use
  2. If the "addons.tags" array field only contains a single element inside of it within food_addons collection. I understand based off the sample document there is only a single value within but want to confirm if there are multiple values for other documents within this collection.
  3. The output from the aggregation operation you ran in regards to the statement “I wrote this query but seemed not to work.”

Additionally, I had done some testing via mongosh on my own test environment using the same 2 sample documents put in collections foodtags and foodaddons. Please see the output below, is this your expected output?

fooddb>db.foodaddons.aggregate([
{ '$project': { _id: 1, outlet_id: 1, addons: 1 } },
{
  '$lookup': {
    from: 'foodtags',
    localField: 'addons.tags',
    foreignField: 'tags._id',
    as: 'tagss'
  }
}
])

Output:

[
  {
    _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'
      }
    ],
    tagss: [
      {
        _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'
      }
    ]
  }
]

Regards,
Jason

1 Like