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

57
Visualizações
copying values inside a record from one key to other in mongodb

I am working on MongoDB. I am trying to append some new key-value pairs to my existing collection. I got more than 50 documents in my collection, in all of which I want to update a new key value pair. In the new key value pair I am adding, one of the values of the key should be from one of the existing key value pair of the same collection. For example:

{ name : "algebra", quantity : 25 }

Now I want to update each of the document with

{ category : "maths" , available_quantity : 25 }

Without entering distinctly for every document its available quantity. I want it to be picked from it's respective quantity's value, and be inserted in the new key available_quantity. I am using the code to update the document :

db.books.update( {} , { $set : { category : "maths" , available_quantity : `__ ? __` }, false, true }

What best can I put in __ ? __ block?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can do it with Aggregation framework:

db.books.update({},
[
  {
    "$set": {
      "available_quantity": "$quantity"
    }
  }
],
{
  multi: true
})

Working example

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