Showing posts with label mongoDB. Show all posts
Showing posts with label mongoDB. Show all posts

Sunday, 30 March 2014

get inserted objectId and find record w.r.t objectId in mongoDB using nodeJS

db.collectionName.insert(rows, function(err,docsInserted){
                    var pKey = new Array() 
                    for(var j=0; j<docsInserted.length; j++){
                       var objk = docsInserted[j];
                        for (var keys in objk)
                    {
                        var attrName = keys;
                        var attrValue = objk[keys];
                        if (attrName == "_id") {
                         pKey.push(attrValue);
                        }
                    }
                    }

db.collectionName.update({'_id': {"$in":pKey}}, {$set: {}},{multi:true}, function(err1, updated) {
                                if (err1 || !updated) {
                                    console.log("Not updated");
                                    console.log(err1);
                                } else {
                                    console.log("Updated");
                               }
});