Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

mongodb updateOne updates document but it returns like it didn't

I’ve been building a backend app with NodeJS and MongoDB.

Until today, I could successfully check every result of updateOne function.

But now, it returns this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{"acknowledged":false,"modifiedCount":0,"upsertedId":null,"upsertedCount":0,"matchedCount":0}

The problem is, it returns this but it updates the document? How can I solve this bug?

>Solution :

From mongoDB documentation:

The method returns a document that contains:

  • matchedCount containing the number of matched documents
  • modifiedCount containing the number of modified documents
  • upsertedId containing the _id for the upserted document.
  • A boolean acknowledged as true if the operation ran with write concern or false if write concern was disabled

So you need to check modifiedCount for check the numeber of document you updateting.

If you need to update just one document i suggest to use findOneAndUpdate(filter, update)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading