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

154
Visualizações
update the nested array in mongodb using node js

I need to update a particular element of the nested array in mongoDB

My mongoDB data looks like below. I need to match the value accessid and name to update the status. The input content has

{"accessid" : 1627047023995, "name" : Name 09, "status" : 100 }

The input content may belong to any level

  {
        "_id" : ObjectId("60fac46ffcbf5287248460a9"), 
        "levelone" : [ 
            {
                "level" : [ 
                    {
                        "name" : "Name 01",
                        "status" : 5
                    }, 
                    {
                        "name" : "Name 02",
                        "status" : 0
                    }, 
                    {
                        "name" : "Name 03",
                        "status" : 0
                    } 
                ], 
                "accessid" : "1627047023995" 
            }, 
            {
                "level" : [ 
                    {
                        "name" : "Name 09",
                        "status" : 5
                    }, 
                    {
                        "name" : "Name 15",
                        "status" : 3
                    } 
                ], 
                "accessid" : "1627047023995" 
            }
        ],
        "createdAt" : ISODate("2021-07-23T13:30:23.995Z") 
    }

I have tried to update the status, but it is updating only the first index value - name: Name 01 status. Please guide to resolve the issue.

collections.updateOne({
  'levelone.level.accessid': accessid,
  'levelone.level.name': name
}, { '$set': { 'levelone.$.level.status': status } }).exec();
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

you can use arrayFilters positional update,

  • query with $elemMatch to filter the main document
  • arrayFilters to define a variable for accessid and b for name
await collections.updateOne({
  levelone: {
    $elemMatch: {
      accessid: accessid,
      "level.name": name
    }
  }
},
{
  $set: {
    "levelone.$[a].level.$[b].status": status
  }
},
{
  arrayFilters: [
    { "a.accessid": accessid },
    { "b.name": name }
  ]
})

Playground

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