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

198
Visualizações
MongoDB: $mergeObjects is overwriting object data in update pipeline

When using $mergeObjects in update pipeline, the original object is being overwritten. Are there any alternatives to avoid this from happening or can $mergeObjects be used in any way to avoid this.

I have a document as follows:

{
    "employment": {
      "selfEmployed": {
          "netProfit": 1000,
          "addbacks": [
              {
                "addbackId": "5a934e000102030405000001",
                "value": 1000,
              }
            ]
      }
    }
}

I have update data as follows:

const update = {
    "selfEmployed": {
        "netProfit": 22,
    }
  };

The update query is:

db.collection.update({},
[
  {
    $set: {
      "employment": {
        $mergeObjects: [
          "$employment",
          update
        ]
      }
    }
  }
])

However $mergeObjects overwrites the object to give following result:

  {
    "employment": {
      "selfEmployed": {
        "netProfit": 22
      }
    }
  }

Expected Result:

{
    "employment": {
      "selfEmployed": {
          "netProfit": 22,
          "addbacks": [
              {
                "addbackId": "5a934e000102030405000001",
                "value": 1000,
              }
            ]
      }
    }
}

Playground

Edit 1:

I can only rely on top level field to perform update. Some answers below do work for selfEmployed. But I have other fields on the collection as well. e.g.

{
    "employment": {
      "selfEmployed": {
          "netProfit": 1000,
          "addbacks": [
              {
                "addbackId": "5a934e000102030405000001",
                "value": 1000,
              }
            ]
      },
      "unemployed": {
          "reason": "some reason",
          //... other data
      }
      // .. other data
    }
}

And i don't want to repeat or write multiple queries for updating nested fields and using direct set is out of the question as well. I'm looking for a way using the update pipeline as I have given above.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

According to $mergeObjects Behaviour,

$mergeObjects overwrites the field values as it merges the documents. If documents to merge include the same field name, the field, in the resulting document, has the value from the last document merged for the field.

Hence, the original employment.selfEmployed will be replaced with the new value.


Solution 1: Direct $set value for employment.selfEmployed.netProfit.

db.collection.update({},
[
  {
    $set: {
      "employment.selfEmployed.netProfit": 22
    }
  }
])

Sample Mongo Playground for Solution 1


Solution 2: $mergeObjects for employment.selfEmployed only.

db.collection.update({},
[
  {
    $set: {
      "employment.selfEmployed": {
        $mergeObjects: [
          "$employment.selfEmployed",
          {
            "netProfit": 22
          }
        ]
      }
    }
  }
])

Sample Mongo Playground for Solution 2

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