I may be missing something but the only thing I can really see is in the result of the new Batch operations API.
var mongodb = require('mongodb'),
MongoClient = mongodb.MongoClient;
MongoClient.connect("mongodb://localhost/test",function(err,db) {
var col = db.collection("mytest");
var batch = col.initializeOrderedBulkOp();
batch.find({ "user": "me" }).upsert().updateOne({ "$addToSet": { "list": 5 } });
batch.execute(function(err,result) {
console.log( JSON.stringify( result, undefined, 4 ) );
});
});
On that sample listing the first time you execute the contents of "result" will dump like this:
{
"ok": 1,
"writeErrors": [],
"writeConcernErrors": [],
"nInserted": 0,
"nUpserted": 1,
"nMatched": 0,
"nModified": 0,
"nRemoved": 0,
"upserted": [
{
"index": 0,
"_id": "5397ae995f04804cbeb7c663"
}
]
}
The notable keys there being "nUpserted" and the "upserted" array for each document that was created. It is a batch operation afterall even though we are doing it once.
On the second execution where it should find the existing document and also match the set member you would get this:
{
"ok": 1,
"writeErrors": [],
"writeConcernErrors": [],
"nInserted": 0,
"nUpserted": 0,
"nMatched": 1,
"nModified": 0,
"nRemoved": 0,
"upserted": []
}
That shows that while the document was matched nothing was actually modified as the set member was the same. But if you change the value of what is applied in the $addToSet
operation to something that doesn't exist then the response would be this:
{
"ok": 1,
"writeErrors": [],
"writeConcernErrors": [],
"nInserted": 0,
"nUpserted": 0,
"nMatched": 1,
"nModified": 1,
"nRemoved": 0,
"upserted": []
}
This shows both the "nMatched" and "nModified" values indicating the document was actually updated by the $addToSet
operation.
So that may be a possible approach