0

I have a function adding a bunch of data into db through $addToSet, and I require a confirmation if the data has been added or not. Since $addToSet does not add duplicates, I want to know if a duplicate has not been added (show an error to the user) or if a db entry has been added (show confirmation to user).

I basically need a callback for the $addToSet operation. Couldnt find it in the docs. New to mongodb. Would appreciate help.

_notifications.update(
        {'username': data.username}, 
        { $addToSet: pushNotification }, function(err, docs){
            console.log(docs);
            if (docs == 0){
                co_notifications.insert(
                    {'username': data.username, 'notifications': insertNotification}, function(er, doc){
                });
            }
        },
        { upsert: true }
    );
Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
amit
  • 10,133
  • 22
  • 72
  • 121
  • Possible dupe of: http://stackoverflow.com/questions/23544366/how-to-check-if-mongos-addtoset-was-a-duplicate-or-not – JohnnyHK Jun 11 '14 at 01:20

1 Answers1

1

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

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317