Hi
I wonder if someone can give me a hand on the following problem. I've implemented some code to update a collection using Collection#updateOne(). Because of the schema of documents in the collection I sometimes have to call updateOne() twice,
with the second call conditional on the result of the first call. A bird's-eye view of the code looks something like this:
my_collection.updateOne(firstQueryDoc, firstUpdateDoc, {upsert: false}, function(err, result){
//...
if (result && result.modifiedCount === 0) {
//...
my_collection.updateOne(secondQueryDoc, secondUpdateDoc, {upsert: true}, function(err, result) {
});
}
});
I prototyped my concept in a stand-alone Node.js script in which I only depend on mongodb module v2.2.25. Btw my Node.js version is 4.5.0 and mongodb server is at v3.0.12. In short my prototype script runs as expected where the second updateOne() call is conditional on result.modifiedCount of the first call.
Now I inject the prototype snippet into an API code base. I there depend on mongodb v2.2.25 as well as other modules such as mongoose v4.9.1.
However, running the API with the snippet injected and towards the same database as before gives a different result object in the updateOne() callback. This result object consistently seems to have the following subset of properties:
{
modifiedCount: undefined,
matchedCount: undefined,
upsertedCount: 0,
result:
{
ok: 1,
nModified: undefined,
n: undefined
}
}
I.e. the result properties are stale at the outcome above whether an update was carried out in the first updateOne() call or not. What is more I can even look at the result object of the
second updateOne() call. I also there find a result object in the callback with the same property values are in the first one.
I also observe that in the first updateOne() call the database is updated as expected. It just seems that the result object in the callback does not represent what was done in the database.
My suspicion is that there is something from other imported modules that affects the result object in the callback of updateOne(). Mongoose v4.9.1. does, however, depend on the same version of mongodb as I have as direct dependency in my API and prototype script.
Is there anyone who may shed some light on what is happening here? Also any ideas on how to debug this problem further and what to look for would be appreciated.
Regards,
Jens Ivar