In our application, we make large amounts of attachments / updates (from 1 to 100 thousand), and I noticed that not all records are saved. It saves from 90% to 95% of records with safemode disabled.
Running upsert with safemode on successful update of all records, but too slow. I remember reading somewhere that even with a safe time there should not be any reason why updating / inserting should fail if the server is unavailable.
I wrote a small application to test this, and included the code below. He is trying to insert 100,000 ints into Mongo, and after checking after starting, I see about 90,000 entries in the collection.
(Note: I use Parallel update, because I update _id, and Mongo 2.0 supports parallel operations when using _id. If you do not use Parallel.Foreach, I still see the loss of records, although not so great)
MongoServer server = MongoServer.Create(host); MongoDatabase test = server.GetDatabase("testDB"); var list = Enumerable.Range(0, 100000).ToList(); using (server.RequestStart(test)) { MongoCollection coll = test.GetCollection("testCollection"); Parallel.ForEach(list, i => { var query = new QueryDocument("_id", i); coll.Update(query, Update.Set("value",100), UpdateFlags.Upsert, SafeMode.False);; }); }
So, I think, my question is: what is the best thing to do with a lot of updates quickly, with 100% success?
I cannot use insertion because I have several processes writing Mongo and cannot be sure if any specific document exists or not, therefore I use Upsert.