Note that there are some explanatory texts on larger screens.

plurals
  1. POMongoDB C# collection.Save vs Insert+Update
    primarykey
    data
    text
    <p>From the C# documentation: </p> <blockquote> <p>The Save method is a combination of Insert and Update. If the Id member of the document has a value, then it is assumed to be an existing document and Save calls Update on the document (setting the Upsert flag just in case it actually is a new document after all).</p> </blockquote> <p>I'm creating my IDs manually in a base class that all my domain objects inherit from. So all my domain objects have an ID when they are inserted into MongoDB.</p> <p>Questions is, should I use collection.Save and keep my interface simple or does this actually result in some overhead in the Save-call (with the Upsert flag), and should I therefor use collection.Insert and Update instead?</p> <p>What I'm thinking is that the Save method is first calling Update and then figures out that my new object didn't exist in the first place, and then call Insert instead. Am I wrong? Has anyone tested this?</p> <p>Note: I insert bulk data with InsertBatch, so big datachunks won't matter in this case.</p> <p><strong>Edit, Follow up</strong></p> <p>I wrote a small test to find out if calling Update with Upsert flag had some overhead so Insert might be better. Turned out that they run at the same speed. See my test code below. MongoDbServer and IMongoDbServer is my own generic interface to isolate the storage facility.</p> <pre><code>IMongoDbServer server = new MongoDbServer(); Stopwatch sw = new Stopwatch(); long d1 = 0; long d2 = 0; for (int w = 0; w &lt;= 100; w++) { sw.Restart(); for (int i = 0; i &lt;= 10000; i++) { ProductionArea area = new ProductionArea(); server.Save(area); } sw.Stop(); d1 += sw.ElapsedMilliseconds; sw.Restart(); for (int i = 0; i &lt;= 10000; i++) { ProductionArea area = new ProductionArea(); server.Insert(area); } sw.Stop(); d2 += sw.ElapsedMilliseconds; } long a1 = d1/100; long a2 = d2/100; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload