Home > Mobile >  mongodb updateOne updates document but it returns like it didn't
mongodb updateOne updates document but it returns like it didn't

Time:03-18

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:

{"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?

CodePudding user response:

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)

  • Related