Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

265
Vistas
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?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You can do it with Aggregation framework:

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

Working example

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda