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

176
Visualizações
How do I make two operations on the same array element?

I have MongoDB users collection with the (simplified) document structure:

{
  _id: ObjectId,
  name: 'John Doe',
  sportsList: [ // array of objects
    {
      name: 'football',
      level: 1500,
      levelHistory: [ // array of objects
        {
          from: 1450,
          to: 1500,
          date: Date
        },
        ...
      ]
    },
    {
      name: 'tennis',
      level: 1400,
      levelHistory: [ // array of objects
        {
          from: 1350,
          to: 1400,
          date: Date
        },
        ...
      ]
    },
  ]
}

I want to update the element of sportsList that has the name === 'tennis' like this:

  • push a new element into levelHistory
  • update the level So far I've managed to push a new element into levelHistory with this query:
await User.updateOne(
  {
    _id: userId,
    sportsList: {
      $elemMatch: { name: 'tennis' }
    }
  },
  {
    $push: {
      'sportsList.$.levelHistory': {
        date: Date,
        from: 1400,
        to: 1450
      }
    },
    // how do I set 'level: 1450' in the same query?
    // if I tried to use a $set: { 'sportsList.$.level': 1450 } mongoDB throws an error
  }
)
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Query

  • pipeline update requires MongoDB >= 4.2
  • map on the array
  • if name is not tennis don't change the element
  • else merge the element(embedded document) with a document that has the new level, and the new levelHistory (the concat of the old, with the new element)

PlayMongo

update(
{"_id": 1},
[{"$set": 
   {"sportsList": 
     {"$map": 
       {"input": "$sportsList",
        "in": 
        {"$cond": 
          [{"$ne": ["$$this.name", "tennis"]}, "$$this",
           {"$mergeObjects": 
           ["$$this",
            {"level": 1450,
             "levelHistory": 
                {"$concatArrays": 
                  ["$$this.levelHistory",
                  [{"from": 1400, "to": 1450, "date": 4}]]}}]}]}}}}}])
about 4 years ago · Juan Pablo Isaza 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