4

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?

Community
  • 1
  • 1
mohoff
  • 321
  • 2
  • 9
  • The correct answer there was the one that referenced the Bulk Update operations since that is the only write result that will actually tell you if the document was modified by the operation of if the "set member" already existed so the document was not modified. You can use this in "server side" code in meteor, but you cannot write the direct call in your "client" code since it is not supported there, and likely should not be. Look for questions wrapping operations like `.aggregate()` with `.publish()` for examples of using "publish" to expose operations to be called from the client. – Neil Lunn Mar 06 '15 at 03:32
  • @mohoff Have you figured this one out? – Gaelan Apr 20 '15 at 23:56
  • @Gaelan No, I changed the code and model around it, so I don't rely on that check anymore. But it might work with the new Meteor 1.1 release in which MongoDB 3.0 is supported. Can you test it? I can't reproduce easily, sorry – mohoff Apr 22 '15 at 18:08

1 Answers1

0

If I re-phrase the question, you're trying to figure if what you're about to insert is already there.

Why not simply query the object for it before attempting to insert?

If you have a set (array) you can simply do

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

and see if it returns an object or nothing. Then update as necessary.

MrE
  • 19,584
  • 12
  • 87
  • 105