MongoDB & C Sharp: CRUD Operations Tutorial
Rate this quickstart
In this Quick Start post, I'll show how to set up connections between C# and MongoDB. Then I'll walk through the database Create, Read, Update, and Delete (CRUD) operations. As you already know, C# is a general-purpose language and MongoDB is a general-purpose data platform. Together, C# and MongoDB are a powerful combination.
The tools and versions I'm using for this series are:
- MongoDB Atlas with an M0 free cluster,
- MongoDB Sample Dataset loaded, specifically the
- Windows 10,
- Visual Studio Community 2019,
- MongoDB BSON Library: version 2.9.1.
C# is a popular language when using the .NET framework. If you're going to be developing in .NET and using MongoDB as your data layer, the C# driver makes it easy to do so.
To follow along, I'll be using Visual Studio 2019 on Windows 10 and connecting to a MongoDB Atlas cluster. If you're using a different OS, IDE, or text editor, the walkthrough might be slightly different, but the code itself should be fairly similar. Let's jump in and take a look at how nicely C# and MongoDB work together.
For this demonstration, I've chosen a Console App (.NET Core), and I've named it
MongoDBConnectionDemo. Next, we need to install the MongoDB Driver for C#/.NET for a Solution. We can do that quite easily with . Inside Visual Studio for Windows, by going to Tools -> NuGet Package Manager -> Manage NuGet Packages for Solution... We can browse for MongoDB.Driver. Then click on our Project and select the driver version we want. In this case, the is 2.9.1. Then click on Install. Accept any license agreements that pop up and head into
Program.csto get started.
To use the
MongoDB.Driverwe need to add a directive.
The first step is to pass in the MongoDB Atlas connection string into a MongoClient object, then we can get the list of databases and print them out.
When we run the program, we get the following out showing the list of databases:
The whole program thus far comes in at just over 20 lines of code:
With a connection in place, let's move on and start doing CRUD operations inside the MongoDB Atlas database. The first step there is to Create some data.
MongoDB stores data in JSON Documents. Actually, they are stored as Binary JSON (BSON) objects on disk, but that's another blog post. In our sample dataset, there is a
gradescollection. Here's what a sample document in that collection looks like:
There are 10,000 students in this collection, 0-9,999. Let's add one more by using C#. To do this, we'll need to use another package from NuGet,
MongoDB.Bson. I'll start a new Solution in Visual Studio and call it
MongoDBCRUDExample. I'll install the
MongoDB.Driverpackages and use the connection string provided from MongoDB Atlas. Next, I'll access our specific database and collection,
collectionvariable is now our key reference point to our data. Since we are using a
BsonDocumentwhen assigning our
collectionvariable, I've indicated that I'm not going to be using a pre-defined schema. This utilizes the power and flexibility of MongoDB's document model. I could define a plain-old-C#-object (POCO) to more strictly define a schema. I'll take a look at that option in a future post. For now, I'll create a new
BsonDocumentto insert into the database.
Then to Create the document in the
sample_training.gradescollection, we can do an insert operation.
If you need to do that insert asynchronously, the MongoDB C# driver is fully async compatible. The same operation could be done with:
If you have a need to insert multiple documents at the same time, MongoDB has you covered there as well with the
We've seen how to structure a BSON Document in C# and then Create it inside a MongoDB database. The MongoDB C# Driver makes it easy to do with the
InsertManyAsync()methods. Now that we have Created data, we'll want to Read it.
You may wonder why we aren't using
Singleas that returns one document too. Well, that has to also ensure the returned document is the only document like that in the collection and that means scanning the whole collection.
Here we're setting a filter to look for a document where the
student_idis equal to
10000. We can pass the filter into the
Find()method to get the first document that matches the query.
If a document isn't found that matches the query, the
Find()method returns null. Finding the first document in a collection, or with a query is a frequent task. However, what about situations when all documents need to be returned, either in a collection or from a query?
For situations in which the expected result set is small, the
ToListAsync()methods can be used to retrieve all documents from a query or in a collection.
Filters can be passed in here as well, for example, to get documents with exam scores equal or above 95. The filter here looks slightly more complicated, but thanks to the MongoDB driver syntax, it is relatively easy to follow. We're filtering on documents in which inside the
scoresarray there is an
examsubdocument with a
scorevalue greater than or equal to 95.
For situations where it's necessary to iterate over the documents that are returned there are a couple of ways to accomplish that as well. In a synchronous situation, a C#
foreachstatement can be used with the
ToEnumerableadapter method. In this situation, instead of using the
ToList()method, we'll use the
This can be accomplished in an asynchronous fashion with the
ForEachAsyncmethod as well:
And we can append the
First()method to that to just get the top student.
The C# Driver for MongoDB provides many ways to Read data from the database and supports both synchronous and asynchronous methods for querying the data. By passing a filter into the
Find()method, we are able to query for specific records. The syntax to build filters and query the database is straightforward and easy to read, making this step of CRUD operations in C# and MongoDB simple to use.
With the data created and being able to be read, let's take a look at how we can perform Update operations.
So far in this C# Quick Start for MongoDB CRUD operations, we have explored how to Create and Read data into a MongoDB database using C#. We saw how to add filters to our query and how to sort the data. This section is about the Update operation and how C# and MongoDB work together to accomplish this important task.
Recall that we've been working with this
BsonDocumentversion of a student record:
After getting part way through the grading term, our sample student's instructor notices that he's been attending the wrong class section. Due to this error the school administration has to change, or update, the
class_idassociated with his record. He'll be moving into section 483.
To update a document we need two bits to pass into an
Updatecommand. We need a filter to determine which documents will be updated. Second, we need what we're wanting to update.
For our example, we want to filter based on the document with
Next, we want to make the change to the
class_id. We can do that with
Then we use the
UpdateOne()method to make the changes. Note here that MongoDB will update at most one document using the
UpdateOne()method. If no documents match the filter, no documents will be updated.
Not all changes are as simple as changing a single field. Let's use a different filter, one that selects a document with a particular score type for quizes:
Now if we want to make the change to the quiz score we can do that with
Set()too, but to identify which particular element should be changed is a little different. We can use the to access the quiz
scorein the array. The $ operator on its own says "change the array element that we matched within the query" - the filter matches with
quizand that's the element will get updated with the set.
And again we use the
UpdateOne()method to make the changes.
If you've been reading along in this blog series I've mentioned that the C# driver supports both sync and async interactions with MongoDB. Performing data Updates is no different. There is also an
UpdateOneAsync()method available. Additionally, for those cases in which multiple documents need to be updated at once, there are
UpdateManyAsync()methods match the documents in the
Filterand will update all documents that match the filter requirements.
Updateis an important operator in the CRUD world. Not being able to update things as they change would make programming incredibly difficult. Fortunately, C# and MongoDB continue to work well together to make the operations possible and easy to use. Whether it's updating a student's grade or updating a user's address, Update is here to handle the changes. The code for the Create, Read, and Update operations can be found in .
We're winding down this MongoDB C# Quick Start CRUD operation series with only one operation left to explore, Delete.
To continue along with the student story, let's take a look at how what would happen if the student dropped the course and had to have their grades deleted. Once again, the MongoDB driver for C# makes it a breeze. And, it provides both sync and async options for the operations.
The first step in the deletion process is to create a filter for the document(s) that need to be deleted. In the example for this series, I've been using a document with a
10000to work with. Since I'll only be deleting that single record, I'll use the
DeleteOne()method (for async situations the
DeleteOneAsync()method is available). However, when a filter matches more than a single document and all of them need to be deleted, the
DeleteManyAsyncmethod can be used.
Here's the record I want to delete.
I'll define the filter to match the
Assuming that we have a
collectionvariable assigned to for the
gradescollection, we next pass the filter into the
If that command is run on the
gradescollection, the document with
10000would be gone. Note here that
DeleteOne()will delete the first document in the collection that matches the filter. In our example dataset, since there is only a single student with a
10000, we get the desired results.
For the sake of argument, let's imagine that the rules for the educational institution are incredibly strict. If you get below a score of 60 on the first exam, you are automatically dropped from the course. We could use a
DeleteOne()to loop through the entire collection, find a single document that matches an exam score of less than 60, delete it, and repeat. Recall that
DeleteOne()only deletes the first document it finds that matches the filter. While this could work, it isn't very efficient as multiple calls to the database are made. How do we handle situations that require deleting multiple records then? We can use
Let's define a new filter to match the exam score being less than 60:
With the filter defined, we pass it into the
With that command being run, all of the student record documents with low exam scores would be deleted from the collection.
This C# Quick Start series has covered the various CRUD Operations (Create, Read, Update, and Delete) operations in MongoDB using basic BSON Documents. We've seen how to use filters to match specific documents that we want to read, update, or delete. This series has, thus far, been a gentle introduction to C Sharp and MongoDB.
BSON Documents are not, however, the only way to be able to use MongoDB with C Sharp. In our applications, we often have classes defining objects. We can map our classes to BSON Documents to work with data as we would in code. I'll take a look at mapping in a future post.
Building a Crypto News Website in C# Using the Microsoft Azure App Service and MongoDB Atlas
Jun 13, 2023