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

319
Views
¿Cómo verificar si el documento actualizado fue realmente modificado?

Tengo una colección de "elementos" y este es un documento de ejemplo en la colección:

 { "id": 0, "price": 23.55 }

Estoy escribiendo una consulta que actualiza el precio de un artículo, pero quiero verificar si el campo de precio realmente se modificó.

Por ejemplo, la siguiente consulta es legal pero en realidad no modifica el documento.

 db.collection('items').updateOne( { id: 0 }, { $set: { price: 23.55 } } );

¿Hay algún método que pueda lograr esto?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

updateOne devolverá nModified , puede saber si el documento se modificó o no.

 { "n": 1, "nModified": 1 }

https://mongodb.github.io/node-mongodb-native/3.6/api/Collection.html#updateOne

https://mongodb.github.io/node-mongodb-native/3.6/api/Collection.html#~updateWriteOpCallback


https://mongodb.github.io/node-mongodb-native/3.6/api/Collection.html#findOneAndUpdate

Use findOneAndUpdate con returnOriginal establecido en falso, obtendrá el documento actualizado en la devolución de llamada.

 const options = { returnOriginal: false; } // to return the updated document findOneAndUpdate(filter, update, options, callback)

https://mongodb.github.io/node-mongodb-native/3.6/api/Collection.html#~findAndModifyCallback

Documento devuelto por el comando findAndModify. Si no se encontraron documentos, el valor será nulo de forma predeterminada (returnOriginal: true), incluso si se alteró un documento; si returnOriginal fuera falso, se devolverá el documento alterado en ese caso.

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!