There are plenty of $addToSet topics out there, but after an hour of searching I still don't know how to evaluate in meteor-serverside-javascript-code if $addToSet added a new element to an array or it was a duplicate match.
The closest one I found was How to check if Mongo's $addToSet was a duplicate or not, but I don't know how to get a db object within meteor.
As written in other posts, the callback-function as a last parameter of the update method always returns 1 and it's always successful, no matter it's a duplicate or distinct element.
If there is no solution currently, I would like to know if there are other ways of checking a nested array (inside one specific collection) for a specific element. A simple true/false result information would be enough.
EDIT:
Ok, I managed to get the Bulk Update working now with the following lines:
var db = MongoInternals.defaultRemoteCollectionDriver().mongo.db;
var col = db.collection("posts");
var batch = col.initializeOrderedBulkOp();
batch.find({_id: postid}).upsert().updateOne({"$addToSet": ...});
batch.execute(function(err, result) {
if (err) throw err;
console.log("RESULT: ", JSON.stringify(result));
//db.close();
});</code>
The $addToSet works with this implementation, but the result
object returned from the execution, still, is always the same:
RESULT: {
"ok":1,
"writeErrors":[],
"writeConcernErrors":[],
"nInserted":0,
"nUpserted":0,
"nMatched":1,
"nModified":null,
"nRemoved":0,
"upserted":[]
}
The interesting value nModified
stays null
both for an update and (duplicate found) skip.
Any ideas?