Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

127
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda