Getting Started With MongoDB and ASP.Net MVC4: Day 2

This article takes you a step further in getting started with MongoDB and ASP.NET series. Today, we will learn how to perform basic CRUD operations in MongoDB using Mongo Shell and a little about MongoDB C# Driver organization. The first two articles in this series are basically focused on MongoDB installation and how to use Mongo Shell to administer Mongo Server. In case you may want to read the Day 1 article, please refer to this link:

Let’s Start
So far you already know how to install and start Mongo Server (if you don’t then please follow the link above). Let’s start using Mongo Shell now and learn how to perform basic DB operations.

Okay, here I’ve a Mongo Server running in my local system on port 27017 and 28017 (the are default ports) as in the following:

And here’s the Mongo Shell:

You can see by looking at this figure that I’ve two collections (tables) in my MongoDB.

Before beginning with the Mongo Shell: there are many administrative and other commands available from the shell. If you want to check out what’s available then just type help and you can get specific help and more specific operations.

Creating Database in MongoDB using Mongo Shell

You already know from the previous article that the default database is “test” in MongoDB. If you want to create another db then just switch the db as you do in SQL Server using “use ” command syntax. It is the same in MongoDB also:

Command: Use myApp” or any name you like, it will switch or create a database if one doesn’t exist already.

Issue the “show dbs” command to see what databases are in there like I can see in the figure above.

Saving (Inserting) documents in MongoDB

As you know, MongoDB is schema-less and you don’t need to worry about creating tables (here we call them Collections), just insert the data into any collection. It will create the collection also if it doesn’t exist; very easy, isn’t it?

A MongoDB Document: Image courtesy (MongoDB Documentation)

MongoDB Collection: Image courtesy (MongoDB Documentation)

You use db..insert() for inserting data/documents into a MongoDB. “db” refers to the active database, so db.collection1 will refer to the current or active database that is myApp in our case. For example:

db.users.insert({username:”sunny”,password:”test”}) //if insert is successful it returns nothing.

The Command above will first create the collection “users” since our database “myApp” is empty and we don’t have any collection in there, and then it will insert the document. Later you can see the data inside a collection using the find() command on a collection.

Select documents from MongoDB

Syntax: db..find() // if you don’t specify any document to filter then it returns all the data.

E.g: db.users.find(); // this will select all documents from the collection.

Did you notice it automatically assigns an id to the document? Yes, it automatically creates a field “_id” with a GUID as a value for your document if it doesn’t contain one. If you’ve a field “_id” specified in your document, it’s pretty good then. You can see in the picture below that if I specify a field “_id” in my document, it doesn’t add it by itself:

It has another add-on function that shows the output of .find() in a formatted JSON format:

Add .pretty() after .find() and it will do the magic:

Deleting document from MongoDB
Syntax: db..remove() // here document contains field(s) that works as filter.

For example: db.users.remove({username:”manish”}) // and you see it’s gone.

So, this was all about giving you a basic idea of how the Mongo Shell works and how to perform administrative tasks using the shell. There’s brilliant documentation available on all the available commands with a nice tutorial on CRUD operations.

I suggest you refer to them if you want to master them.

The official MongoDB Driver for C#
Brief History
NoRM was popular for a while but now it’s no longer maintained hence not being used in new applications. There are also mongodb-csharp and simple-mongodb but they’ve both been inactive for several years. FluentMongo is the first library to provide LINQ support. Now that the LINQ support is built into the official driver, most of the apps have migrated to the official driver completely.

Ways to get/install the official MongoDB driver for C#
  1. The easiest and most popular is through Nuget. If you’re using Visual Studio, then right-click on your project in Solution Explorer and click “Manage Nuget Packages…” then search for “mongo” under the online tab and you’ll see “Official mongoDB C# Driver” listed on top. Click “Install” and wait for the installation to finish.

    If you use the Package Manager Console then you may want to install it using this command:

    Install-Package mongocsharpdriver

  2. There’s also a Zip archive with every release if you want to download them.
  3. Also there’s an MSI installer in case you want to do a Windows style installation from a setup file.
Here you can find the Zip archive and the MSI files: C#/.NET Driver Version 1.9.0 Release Notes

The purpose of this article was to teach MongoDB basic administrative operations using Mongo Shell and how to download/install the MongoDB C# official Driver. Since we’re going to learn how to use MongoDB in ASP.NET MVC, we will be using the MongoDB C# Official Driver instead of the Mongo Shell. On the next day, we will learn about the official MongoDB C# Driver’s organization and how it connects to a Mongo Server.

I hope you enjoyed this post… thanks for reading!!


Leave a Reply

Your email address will not be published. Required fields are marked *