Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB C# collection.Save vs Insert+Update

From the C# documentation:

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).

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.

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?

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?

Note: I insert bulk data with InsertBatch, so big datachunks won't matter in this case.

Edit, Follow up

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.

IMongoDbServer server = new MongoDbServer();
Stopwatch sw = new Stopwatch();
long d1 = 0;
long d2 = 0;
for (int w = 0; w <= 100; w++)
{
    sw.Restart();
    for (int i = 0; i <= 10000; i++)
    {
        ProductionArea area = new ProductionArea();
        server.Save(area);
    }
    sw.Stop();
    d1 += sw.ElapsedMilliseconds;
    sw.Restart();
    for (int i = 0; i <= 10000; i++)
    {
        ProductionArea area = new ProductionArea();
        server.Insert(area);
    }
    sw.Stop();
    d2 += sw.ElapsedMilliseconds;
}
long a1 = d1/100;
long a2 = d2/100;
like image 636
Paw Baltzersen Avatar asked Nov 21 '11 21:11

Paw Baltzersen


People also ask

Can you use MongoDB with C#?

By developing with C# and MongoDB together one opens up a world of possibilities. Console, window, and web applications are all possible. As are cross-platform mobile applications using the Xamarin framework.

What is a MongoDB driver?

The official MongoDB Node. js driver allows Node. js applications to connect to MongoDB and work with data. The driver features an asynchronous API which allows you to interact with MongoDB using Promises or via traditional callbacks.

Is MongoDB paid?

Is MongoDB Free? You can get started with a MongoDB developer sandbox in MongoDB Atlas for free with basic configuration options. No credit cards are required to provision a cluster, and you can use it to explore and learn more about MongoDB Atlas, the database-as-a-service platform from MongoDB.


1 Answers

The Save method is not going to make two trips to the server.

The heuristic is this: if the document being saved does not have a value for the _id field, then a value is generated for it and then Insert is called. If the document being saved has a non-zero value for the _id, then Update is called with the Upsert flag, in which case it is up to the server to decide whether to do an Insert or an Update.

I don't know if an Upsert is more expensive than an Insert. I suspect they are almost the same and what really matters is that either way it is a single network round trip.

If you know it's a new document you might as well call Insert. And calling InsertBatch is way more performant than calling many individual Inserts. So definitely prefer InsertBatch to Save.

like image 191
Robert Stam Avatar answered Sep 26 '22 21:09

Robert Stam