I have a collection of events elements 2.502.011 and you want to update for all elements. Unfortunately, I encountered many Mongodb errors due to write blocking.
Question: How can I avoid these errors to ensure that all my events have been updated correctly?
Here is information about my event collection:
> db.events.stats() { "count" : 2502011, "size" : 2097762368, "avgObjSize" : 838.4305136947839, "storageSize" : 3219062784, "numExtents" : 21, "nindexes" : 6, "lastExtentSize" : 840650752, "paddingFactor" : 1.0000000000874294, "systemFlags" : 0, "userFlags" : 0, "totalIndexSize" : 1265898256, "indexSizes" : { "_id_" : 120350720, "destructured_created_at_1" : 387804032, "destructured_updated_at_1" : 419657728, "data.assigned_author_id_1" : 76053152, "emiting_class_1_data.assigned_author_id_1_data.user_id_1_data.id_1_event_type_1" : 185071936, "created_at_1" : 76960688 } }
Here's what the event looks like:
> db.events.findOne() { "_id" : ObjectId("4fd5d4586107d93b47000065"), "created_at" : ISODate("2012-06-11T11:19:52Z"), "data" : { "project_id" : ObjectId("4fc3d2abc7cd1e0003000061"), "document_ids" : [ "4fc3d2b45903ef000300007d", "4fc3d2b45903ef000300007e" ], "file_type" : "excel", "id" : ObjectId("4fd5d4586107d93b47000064") }, "emiting_class" : "DocumentExport", "event_type" : "created", "updated_at" : ISODate("2013-07-31T08:52:48Z") }
I would like to update each event to add 2 new field bases to existing created_at and updated_at . Please correct me if I am wrong, but it seems that you cannot use the mongo update command when you need to access the current item data along the way.
This is my update loop:
db.events.find().forEach( function (e) { created_at = new Date(e.created_at); updated_at = new Date(e.updated_at); e.destructured_created_at = [e.created_at];
When executing the above command, I get a huge number of page errors due to a write lock in the database.
