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

238
Visualizações
how to add values using mongodb aggregation

is there any way to add values via aggregation like db.insert_one

x = db.aggregate([{
    "$addFields": {
        "chat_id": -10013345566,
    }
}])

i tried this but this code return nothing and values are not updated

i wanna add the values via aggregation cuz aggregation is way faster than others

sample document :

    {"_id": 123 , "chat_id" : 125}
    {"_id": 234, "chat_id" : 1325}
    {"_id": 1323 , "chat_id" : 335}

expected output :

alternative to db.insert_one() in mongodb aggregation

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

0

You have to make use of $merge stage to save output of the aggregation to the collection.

Note: Be very very careful when you use $merge stage as you can accidentally replace the entire document in your collection. Go through the complete documentation of this stage before using it.

db.collection.aggregate([
  {
    "$match": {
      "_id": 123
    }
  },
  {
    "$addFields": {
      "chat_id": -10013345566,
      
    }
  },
  {
    "$merge": {
      "into": "collection",  // <- Collection Name
      "on": "_id",  // <- Merge operation match key
      "whenMatched": "merge"  // <- Operation to perform when matched
    }
  },
])

Mongo Playground Sample Execution

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