简体   繁体   中英

Querying a Nested Document using an array - MongoDB

I have a Document like this

{
"_id" : ObjectId("61e10561a06dd136bc848b1f"),
"packagingCharges" : {
    "small" : 0,
    "medium" : 0,
    "large" : 0
},
"themeColor" : "#168EBA",
"openingTime" : "9 am",
"closingTime" : "9 pm",
"discount" : "0",
"isRestaurantActive" : true,
"restaurantName" : "Aman Restaurant",
"address" : "Faridabad",
"createdBy" : ObjectId("61dd2182871d7c2be8b41006"),
"restaurantTokenImage" : "..\\tmp\\uploads\\RestaurantImages\\1642136929834_999192.jpg",
"restaurantLogoImage" : "..\\tmp\\uploads\\RestaurantImages\\1642136929928_doreMON.JPG",
"coins" : 0,
"isGstRegistered" : true,
"offers" : [ 
    {
        "isAvailable" : true,
        "_id" : ObjectId("61e105ed202546421025c221"),
        "giftItem" : "Pizza",
        "coins" : 10,
        "menuId" : ObjectId("61e105ed202546421025c220")
    }
],
"menu" : [ 
    {
        "menuItem" : "chapati",
        "price" : 5,
        "menuItemImageName" : "979696859_054.jpg",
        "menuItemImage" : "..\\tmp\\uploads\\menuItemImages\\1642136982717_979696859_054.jpg",
        "isGift" : false,
        "isAvailable" : true,
        "excludeMe" : true,
        "_id" : ObjectId("61e10596a06dd136bc848b20"),
        "coins" : 1
    }, 
    {
        "menuItem" : "Pizza",
        "price" : 50,
        "menuItemImageName" : "979696859_054.jpg",
        "menuItemImage" : "..\\tmp\\uploads\\menuItemImages\\1642137069230_979696859_054.jpg",
        "isGift" : true,
        "isAvailable" : true,
        "excludeMe" : true,
        "_id" : ObjectId("61e105ed202546421025c220"),
        "coins" : 10
    }, 
    {
        "menuItem" : "Pasta",
        "price" : 80,
        "menuItemImageName" : "979696859_054.jpg",
        "menuItemImage" : "..\\tmp\\uploads\\menuItemImages\\1642137188767_979696859_054.jpg",
        "isGift" : false,
        "isAvailable" : true,
        "excludeMe" : true,
        "_id" : ObjectId("61e10664202546421025c222"),
        "coins" : 10
    }
],
"createdAt" : ISODate("2022-01-14T05:08:50.032Z"),
"updatedAt" : ISODate("2022-01-14T05:13:08.879Z"),
"__v" : 0

}

I want to find out menus with help of an array [ObjectId("61e10596a06dd136bc848b20"), ObjectId("61e105ed202546421025c220")] and I don't want rest of the fields. What's the best query I can write. I am using mongo as database and javascript or Node for backend

  • $match find document
  • $project keep only menu
  • $unwind separate menu array
  • $match find id _in array
db.collection.aggregate([
  {
    "$match": {
      "menu._id": {
        "$in": [
          ObjectId("61e10596a06dd136bc848b20"),
          ObjectId("61e105ed202546421025c220")
        ]
      }
    }
  },
  {
    "$project": {
      "menu": 1
    }
  },
  {
    "$unwind": "$menu"
  },
  {
    "$match": {
      "menu._id": {
        "$in": [
          ObjectId("61e10596a06dd136bc848b20"),
          ObjectId("61e105ed202546421025c220")
        ]
      }
    }
  }
])

mongoplayground

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