最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Mongoose - How to push object in nested array of objects - Stack Overflow

programmeradmin2浏览0评论

I have this data structure in my MongoDB database:

"menu": [
        {
            "dishCategory":"61e6089f209b802518e2b4a4",
            "dishMeals": [
                {
                    "dishMealName": "Burger King",
                    "dishMealIngredients": "Burger lepinja, bbq sos, berlin sos, zelena"
                    "dishMealPrice": 5
                }
            ]
        }
    ]

How do I push a new object inside dishMeals in exact dishCategory ( I am providing dishCategoryId, newDish object and _id of restaurant through req.body) I've tried this but nothing is changing:

await Restaurants.updateOne(
        {
            '_id' : _id,
            'menu.dishCategory' : dishCategoryId
        },
        {
            $push : {
                'menu.$.dishMeals' : newDish
            }
        }
    );

I have this data structure in my MongoDB database:

"menu": [
        {
            "dishCategory":"61e6089f209b802518e2b4a4",
            "dishMeals": [
                {
                    "dishMealName": "Burger King",
                    "dishMealIngredients": "Burger lepinja, bbq sos, berlin sos, zelena"
                    "dishMealPrice": 5
                }
            ]
        }
    ]

How do I push a new object inside dishMeals in exact dishCategory ( I am providing dishCategoryId, newDish object and _id of restaurant through req.body) I've tried this but nothing is changing:

await Restaurants.updateOne(
        {
            '_id' : _id,
            'menu.dishCategory' : dishCategoryId
        },
        {
            $push : {
                'menu.$.dishMeals' : newDish
            }
        }
    );
Share Improve this question edited Jul 20, 2022 at 0:43 Yong Shun 51.8k6 gold badges35 silver badges63 bronze badges asked Jan 19, 2022 at 23:56 mne_web_devmne_web_dev 2515 silver badges14 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4
  1. Use arrayFilters to filter the nested document(s) in the array field, then $push if the filter criteria in the arrayFilters met.
db.collection.update({
  "_id": _id,
  "menu.dishCategory": dishCategoryId
},
{
  $push: {
    "menu.$[menu].dishMeals": newDish
  }
},
{
  arrayFilters: [
    {
      "menu.dishCategory": dishCategoryId
    }
  ]
})

Sample Mongo Playground

You can do it with arrayFilters config in update query:

db.collection.update({
  "restaurant_id": 1
},
{
  "$push": {
    "menu.$[element].dishMeals": {
      "dishMealName": "Best Burger",
      "dishMealIngredients": "Best burger in town",
      "dishMealPrice": 10
    }
  }
},
{
  "arrayFilters": [
    {
      "element.dishCategory._id": "61e6089f209b802518e2b4a4"
    }
  ]
})

Working example

You may read the question and the solution they provided here, Hope this one will be helpful to you.

db.collection.update({
    "_id": 1,
    "menu.dishCategory": "61e6089f209b802518e2b4a4"
},
{
    $push: {
        "menu.$.dishMeals": newMeal
    }
})

Sample Example

发布评论

评论列表(0)

  1. 暂无评论