0

I am trying update a bulk of data in Mongo DB. Here is what I am trying to do:

var bkdet = db.library.find();
bkdet.forEach(function(item){
//process item
    var rentDateWithoutTimestamp = item.book.rentDate;
    if(rentDateWithoutTimestamp!=null){
        var dt = rentDateWithoutTimestamp.split(" ");
        print(dt[0]+"" +item._id);
        db.library.update({"_id": item._id}, {$set: {"book.rentDate": dt[0]}});
    }

})

Weird part is, it only updated around 3000 records.

Initially I tried:

db.library.update({"_id": item._id}, {$set: {item.book.rentDate": dt[0]}});

It printed the log that it updated 1 existing record, but no data change happened. That is when I tried by removing item. from item.book.rentDate and it did not complete all the records as expected.

Hani
  • 394
  • 4
  • 11
crazyProgrammer
  • 65
  • 1
  • 2
  • 7
  • Are you sure item.book.rentDate is not NULL in all the records? If it is NULL, it won't be updated. – RLD Sep 24 '17 at 04:47
  • It is not null. All the records have data – crazyProgrammer Sep 24 '17 at 04:56
  • Can you provide sample document of your Bulk data that you are trying to update? – RLD Sep 24 '17 at 05:03
  • You should look into using the [mongo bulk](https://docs.mongodb.com/manual/reference/method/db.collection.initializeUnorderedBulkOp/) operations API. Doing one off updates in a tight loop like this can cause performance issues. I would look at your mongo logs to see if they have errors in them that are preventing the operations from completing. – ThrowsException Sep 24 '17 at 14:25

0 Answers0