Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding the changes in MongoDB new C# driver (Async and Await)

The new C# driver is totally Async and in my understanding twists a little bit the old design patterns such as DAL in n-tier architecture.

In my Mongo DALs I use to do:

public T Insert(T entity){
     _collection.Insert(entity);
     return entity;
}

This way I can get the persisted ObjectId.

Today, everything is Async such as InsertOneAsync.
How would Insert method will now return the entity when the InsertOneAsync will be done? Can you show an example?

like image 1000
SexyMF Avatar asked May 07 '15 04:05

SexyMF


People also ask

How do I watch for changes to a single MongoDB database?

Starting in MongoDB 4.0, you can open a change stream cursor for a single database (excluding admin, local, and config database) to watch for changes to all its non-system collections. For the MongoDB driver method, refer to your driver documentation.

What is the difference between update and updateone in MongoDB?

The update is a document that specifies the change to apply. The options argument provides some options for updates that won’t be covered in this tutorial. The updateOne () method returns a document that contains some fields. The notable ones are: The matchedCount returns the number of matched documents.

What is the difference between modifiedcount and matchedcount in MongoDB?

The notable ones are: The matchedCount returns the number of matched documents. The modifiedCount returns the number of updated documents. In the case of the updateOne () method, it can be either 0 or 1. The $set operator allows you to replace the value of a field with a specified value.

What is the _Id object in MongoDB?

The _id object has the following form: The _data type depends on the MongoDB versions and, in some cases, the feature compatibility version (fCV) at the time of the change stream's opening/resumption. See Resume Tokens for the full list of _data types.


1 Answers

It's helpful to understand the basics of async / await because it's a somewhat leaky abstraction and has a number of pitfalls.

Essentially, you have two options:

  • Remain synchronous. In this case, it's safe to use .Result and .Wait() on the async calls, respectively, e.g. something like

    // Insert:
    collection.InsertOneAsync(user).Wait();
    
    // FindAll:
    var first = collection.Find(p => true).ToListAsync().Result.FirstOrDefault();
    
  • Go async in your code base. Doing it async is quite 'infectious', unfortunately, so either you convert pretty much everything to async, or not. Careful, mixing sync and async incorrectly will lead to deadlocks. Using async has a number of advantages, because your code can continue to run while MongoDB is still working, e.g.

    // FindAll:
    var task = collection.Find(p => true).ToListAsync();
    // ...do something else that takes time, be it CPU or I/O bound
    // in parallel to the running request. If there's nothing else to 
    // do, you just freed up a thread that can be used to serve another 
    // customer...
    // once you need the results from mongo:
    var list = await task;
    
like image 172
mnemosyn Avatar answered Oct 18 '22 21:10

mnemosyn