MongoDB: $ addToSet duplicates result in Meteor

There are many topics in $ addToSet, but after an hour of searching I still don’t know how to evaluate meteor-serveride-javascript code if $ addToSet adds a new element to the array or it was a duplicate match.

The closest I found was How to check if Mongo $ addToSet was a duplicate or not , but I don't know how to get the db object in the meteor.

As written in other posts, the callback function as the last parameter of the update method always returns 1 and is always successful, regardless of whether it is a repeating or a single element.

If there is currently no solution, I would like to know if there are other ways to check the nested array (inside one specific collection) for a specific element. Simple information on true / false information will suffice.

EDIT:

Ok, I managed to run Bulk Update 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>

$ addToSet works with this implementation, but the object resultreturned from execution still remains unchanged:

RESULT: {
  "ok":1,
  "writeErrors":[],
  "writeConcernErrors":[],
  "nInserted":0,
  "nUpserted":0,
  "nMatched":1,
  "nModified":null,
  "nRemoved":0,
  "upserted":[]
}

An interesting value nModifiedremains nullboth for updating and for duplication of the found.

Any ideas?

+4
source share
1 answer

If I re-formulate the question, you are trying to understand that what you are going to insert is already there.

?

(),

db.collection.find({array_key: object_to_find_in_array})

, . .

0

All Articles