I am using the mpmodb npm module to connect to my mongo database using nodejs.
My simple example: I need to update a document in a collection that may or may not exist.
So, if the document exists, I should be able to update it and send a confirmation in my api response.
If the document does not exist, I must respond with a corresponding message that the document does not exist.
In principle, a very simple use case.
Here is how I update:
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect(url, function(conn_err, db) {
if (conn_err) {
console.log("Error connecting to mongodb server!!");
console.error(conn_err);
} else {
console.log("Connected to mongodb server!!");
var collection = db.collection("test");
collection.updateOne(query, {"$set": data}, {w: "majority"}, function (err, result) {
console.error(err);
console.log(result);
});
}
});
Now in resultthe callback I get an object containing a lot of things, but mainly:
result: { ok: 1, nModified: 1, n: 1 }
...
matchedCount: 1,
modifiedCount: 1,
upsertedId: null,
upsertedCount: 0
Now this is where my problem begins.
1) If the document exists and is updated, I get:
result: { ok: 1, nModified: 1, n: 1 },
...
matchedCount: 1,
modifiedCount: 1,
upsertedId: null,
upsertedCount: 0
2) , ( $set , db), :
result: { ok: 1, nModified: 0, n: 1 },
...
matchedCount: 1,
modifiedCount: 0,
upsertedId: null,
upsertedCount: 0
3) , :
result: { ok: 1, nModified: 0, n: 1 },
...
matchedCount: 1,
modifiedCount: 0,
upsertedId: null,
upsertedCount: 0
, 2) 3)?
.
- ?
:
mongodb v3.0.7,
nodejs v4.2.2,
mongodb npm module v2.0.x