简体   繁体   中英

Mongoose join two collections and get data updated based on other

I'm doing simple restaurant application, got structed in cart module.

I've data from 2 collections namely Menus & cart.

Menu Schema:

menus = [
    {
        "_id": "639ac96eec3db0a1ef2b7490",
        "menu_name": "Burger",
        "menu_type": "Veg",
        "cuisine_type": "American",
        "price": 5,
        "Quantity": 0
    },
    {
        "_id": "639aca45ec3db0a1ef2b7498",
        "menu_name": "Tacos",
        "menu_type": "Non-veg",
        "cuisine_type": "Mexican",
        "price": 3,
        "Quantity": 0
    },
    {
        "_id": "639af103eaa2d49802f66b12",
        "menu_name": "Pizza",
        "menu_type": "Veg",
        "cuisine_type": "Italian",
        "price": 6,
        "Quantity": 0
    },
    {
        "_id": "63a4257efe88505582685fe8",
        "menu_name": "Sushi",
        "menu_type": "Non-veg",
        "cuisine_type": "Japanese",
        "price": 4,
        "Quantity": 0
    },
    {
        "_id": "63a42592fe88505582685fea",
        "menu_name": "Thai Curry",
        "menu_type": "Non-veg",
        "cuisine_type": "Thai",
        "price": 5,
        "Quantity": 0
    }
];

Cart Schema:

cart = [
    {
        "_id": "63b3cf4c5303f351577fd1b9",
        "menuId": "639ac96eec3db0a1ef2b7490",
        "menuQuantity": 3
    },
    {
        "_id": "63b67d08b39f6c7eb335aa30",
        "menuId": "639af103eaa2d49802f66b12",
        "menuQuantity": 1
    }
];

If a Menu item exists in cart, it's quantity count should be updated & if not, the quantity should be zero as it is.

Expected Result:

result = [
    {
        "_id": "639ac96eec3db0a1ef2b7490",
        "menu_name": "Burger",
        "menu_type": "Veg",
        "cuisine_type": "American",
        "price": 5,
        "Quantity": 3
    },
    {
        "_id": "639aca45ec3db0a1ef2b7498",
        "menu_name": "Tacos",
        "menu_type": "Non-veg",
        "cuisine_type": "Mexican",
        "price": 3,
        "Quantity": 0
    },
    {
        "_id": "639af103eaa2d49802f66b12",
        "menu_name": "Pizza",
        "menu_type": "Veg",
        "cuisine_type": "Italian",
        "price": 6,
        "Quantity": 1
    },
    {
        "_id": "63a4257efe88505582685fe8",
        "menu_name": "Sushi",
        "menu_type": "Non-veg",
        "cuisine_type": "Japanese",
        "price": 4,
        "Quantity": 0
    },
    {
        "_id": "63a42592fe88505582685fea",
        "menu_name": "Thai Curry",
        "menu_type": "Non-veg",
        "cuisine_type": "Thai",
        "price": 5,
        "Quantity": 0
    }
]

Appreciate any help. Thanks!

Perform the $lookup then $set the field from $lookup result.

db.menus.aggregate([
  {
    "$lookup": {
      "from": "cart",
      "localField": "_id",
      "foreignField": "menuId",
      "as": "cartLookup"
    }
  },
  {
    $set: {
      Quantity: {
        $sum: "$cartLookup.menuQuantity"
      }
    }
  },
  {
    "$unset": "cartLookup"
  },
  {
    "$merge": {
      "into": "menus",
      "on": "_id",
      "whenMatched": "merge"
    }
  }
])

Mongo Playground

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM