MongoDb 002 : CRUD – Part 1

We will continue with our exploration of MongoDb in this second part of tutorial, working our CRUD examples.

Let’s beging with the ‘C’. In order to create a document, you would need to use, as you can guess, the Insert command.


Notice that we are using a new collection named subject here. MongoDb would automatically create the collection if it doesn’t exist. As you would have rightly guessed by now, we would be relying on JSON for passing any parameters for commands.

Let’s now do the same using C#. The first step you need to do is to install the MongoDB.Driver official Nuget package. Once the nuget is installed for our project, let’s start pushing some code in to our collection.

Following Commands gives us access to

string ConnectionString = "mongodb://localhost:27017";
var Client = new MongoClient(ConnectionString);
var Database = Client.GetDatabase("testdb");

The next step is to create the Data Structure which would be serialized and pushed into the DB.

public class SubjectEntity
public ObjectId Id { get; set; }

public string Title { get; set; }

If you look closely, I have decorated each of the elements with BsonElement Attribute. This tells the compiler the literal it should use as key while creating the JSON document. Also if you notice, I have a property called Id of type ObjectId. This is a special type which would hold the unique ID which would be generated by MongoDb. This value is similiar to a GUID and unlike relational Db, we cannot use an autoincrement key, unless we do that bit of logic within our C# code. Honestly I wouldn’t recommend that.

Since I would like to use the ID generated by MongoDb, I have not decorated the Id field with BsonElement attribute, however I would still need in my Data Structure so that I could use it while retrieving data.

Okay, fine then. Let’s push some data to the Db now.

var subjectCollection = Database.GetCollection("subject");
subjectCollection .InsertOne(SubjectEntityInstance);

The above code inserts a object of type SubjectEntity to the Db. Once executed, hit our Console again and check if the Document is added to the SubjectCollection using the find command we learned in the first part of tutorial. Looks good right ?

Let’s try finding it with a find command from C# now.

var nameFilter = Builders.Filter
.Eq(x => x.Title,'maths');
var result = subjectCollection

As you can understand from the code, we are first creating an instance of the filter we would like to use. In this case, we are using an Equality Filter (Subject title should be “maths”). We finally pass the filter to our FindSync Method.

We would be exploring part two of CRUD, Update and Delete in the second part of the tutorial.

The complete list of beginners guide in this series can be found in here


One thought on “MongoDb 002 : CRUD – Part 1

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s