Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

190
Views
MongoDB: ¿hay alguna forma de realizar $set en un hash sin sobrescribir valores/claves existentes/sin referencia?

Digamos que tengo un documento como este:

 { _id: "12345", hash_field: { "foo": "bar" } }

Y en algún momento, sin saber cuál es el valor del hash_field de este documento, necesito agregarle más pares clave/valor mientras dejo intacto el actual. Estoy buscando una manera de hacer esto sin borrar los datos existentes y sin tener que hacer varias actualizaciones anidadas.

 db.myCollection.updateOne({_id:"12345"}, {'$set': { hash_field: { "key1": "value1", "key2": "value2" } } })

Lo anterior borrará el {"foo":"bar"} .

Sé que puedo hacer dos llamadas $set separadas con '$set': {"hash_field.key1": "value1"} y '$set': {"hash_field.key2": "value2"} pero estoy buscando una forma de hacer esto en una actualización de mongo. ¿Posible?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Usa $mergeObjects .

 db.collection.update({ _id: "12345" }, [ { "$set": { hash_field: { $mergeObjects: [ "$hash_field", { "key1": "value1", "key2": "value2" } ] } } } ])

parque infantil mongo

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!