Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

125
Views
MongoDB update nested item by dynamical ID

Hey i'm trying to update a nested Object in my MongoDB.

They structure is

[
 {
  "_id": "5871010d1ff9831574e7178d",
  "created_at": "2017-01-07T14:54:05.791Z",
  "updated_at": "2017-01-07T14:54:05.791Z",
  "place_id": "ChIJ1eTO6eS1vkcRL_mX2RWUKo4",
  "name": "Kunstwerk Restaurant Gummersbach",
  "__v": 0,
  "cooks": [],
  "menus": [
   {
    "58727068ba8d6c04a0ea331f": [
      {
        "intolerances": {
           "intolerance" : "false"
            ...
          },
        "nutritiondata" : [
          {...}
         ],
         "cookname": "testkoch@test.de",
         "menuname": "MenünameX"
      }
    ]
  }
]

The nested ID in menus is another ID of a MongoDB document which is created dynamicly.

I try to update the menuname, but cant access it, since mongo always returns null or doesnt find the element since in need to search for only the key not the key - value pair.

What i tested so far:

db.restaurants.update({ 'menus.58727068ba8d6c04a0ea331f', { $set : {"menus.$.menuname" : "test"}}});

db.restaurants.findOne({ 'menus.58727068ba8d6c04a0ea331f' : "58727068ba8d6c04a0ea331f"});

db.restaurants.find({ menus : { $elemMatch : {"58727068ba8d6c04a0ea331f": "58727068ba8d6c04a0ea331f" }}});

db.restaurants.findOne({ menus : { $eq : "58727068ba8d6c04a0ea331f" }});
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Use $set method as shown below :

db.restaurants.update({"_id":"5871010d1ff9831574e7178d"}, {$set: {"menus.58727068ba8d6c04a0ea331f.0.menuname":"test"});

This will work only if you know the index of the item in the array that is being updated. Check this Page, it has some good examples on updating multi level nested arrays, hope it will help.

over 4 years ago · Santiago Trujillo Report

0

So thanks to suzo i figgured out the solution. The MongoShell code that works:

db.restaurants.update({"_id" : ObjectId("5871010d1ff9831574e7178d")}, {$set: {"menus.58727068ba8d6c04a0ea331f.0.menuname" : "testName"}});

UPDATE: Since my field is Mixed type, this isn't updating without getting marked!. So here is the complete correct code:

 Restaurant.findOne({ _id : req.body.r_id }, function(error, restaurant){
    if(error) console.log(error);

    for(var i = 0; i < restaurant.menus.length; i++){
          restaurant.markModified('menus');
        if(restaurant.menus[i][req.params.id] != undefined){
            restaurant.menus[i][req.params.id].menuname = req.body.name;
        }

        if(i == restaurant.menus.length-1){
            restaurant.save();
            res.json(jObj);
        }
    }

  })
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!