Writes acknowledged, matched, but not written


We are having an issue when using (multiple) services and our nodejs mongodb driver, (^4.2.1),
is refusing to write after working for a while on a service. (if lucky, sometimes it goes straight to this strange issue).

mongo is not throwing any errors, but when checking the response of a call like this:

this.client = await MongoClient.connect(
  { maxPoolSize: 50 }

const { acknowledged, upsertedCount, modifiedCount, matchedCount } =
  this.client.replaceOne({ _id: doc._id}, doc);

where doc is something like:

{ _id: "123", foo: "bar" }

It gives a response of:

  acknowledged: true,
  upsertedCount: 0,
  modifiedCount: 0,
  matchedCount: 1,

and (with confirming using a gui connected to the database), the document is not being updated.

I’ve tried looking at the documentation, but I cannot seem to find anything as to why this happens.

more context / info

  • service A can have this bug (seemingly at random), while service B operates normally,
  • both services are on the same version of mongo with the same code (the code is wrapped into a private library)
  • both services can be working with the same document (potentially? at the same time (unlikely, but possible)) with different writes to that document (we know this is a potential race condition)

Since matchCount is 1, it is clear that the document is found.

If modifiedCount is 0, then the document has not been modified. It is quite possible if foo was already equal to bar.

What I do not understand for the code snippet you shared is that you call replaceOne on this.client. I was under the impression, and quite sure, that replaceOne requires a collection. But you are calling it directly on a MongoClient object. That is weird. I suspect you did not publish the real code. It is hard to find what you are doing wrong when you publish redacted code.

Sorry, the code is redacted, the code does indeed use: