Quick Start: C# and MongoDB - Starting and Setup

Ken W. Alger

#C#/.NET
C Sharp Quick Start

In the first of the C# Quick Start series, I'll show how to set up connections between C# and MongoDB.

In future parts we'll move on and work through:

  • Database Create, Read, Update, and Delete (CRUD) operations
  • Map MongoDB to C# objects
  • Look at MongoDB aggregation
  • How to use LINQ with MongoDB

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.

To follow along, I'll be using Visual Studio 2019 on Windows 10 and we will be connecting to a MongoDB Atlas cluster.

Get started with an M0 cluster on MongoDB Atlas today. It's free forever and you'll be able to work alongside this blog series.

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.

Getting Setup

For this demonstration, I’ve chosen to create 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 NuGet. 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 latest stable version is 2.9.1. Then click on Install. Accept any license agreements that pop up and head back to Program.cs to get started.

C# and MongoDB Driver Installation

Putting the Driver to Work

To use the MongoDB.Driver we need to add a directive.

using MongoDB.Driver;

Inside the Main() method we’ll establish a connection to MongoDB Atlas with a connection string and to test the connection we’ll print out a list of the databases on the server. The Atlas cluster to which we’ll be connecting has the MongoDB Atlas Sample Dataset installed, so we’ll be able to see a nice database list.

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.

MongoClient dbClient = new MongoClient(<<YOUR ATLAS CONNECTION STRING>>);

var dbList = dbClient.ListDatabases().ToList();

Console.WriteLine("The list of databases on this server is: ");
foreach (var db in dbList)
{
    Console.WriteLine(db);
}

When we run the program, we get the following out showing the list of databases:

The list of databases on this server is: 
{ "name" : "sample_airbnb", "sizeOnDisk" : 57466880.0, "empty" : false }
{ "name" : "sample_geospatial", "sizeOnDisk" : 1384448.0, "empty" : false }
{ "name" : "sample_mflix", "sizeOnDisk" : 45084672.0, "empty" : false }
{ "name" : "sample_supplies", "sizeOnDisk" : 1347584.0, "empty" : false }
{ "name" : "sample_training", "sizeOnDisk" : 73191424.0, "empty" : false }
{ "name" : "sample_weatherdata", "sizeOnDisk" : 4427776.0, "empty" : false }
{ "name" : "admin", "sizeOnDisk" : 245760.0, "empty" : false }
{ "name" : "local", "sizeOnDisk" : 1919799296.0, "empty" : false }

The whole program comes in at just over 20 lines of code:

using System;
using MongoDB.Driver;

namespace test
{
    class Program
    {
        static void Main(string[] args)
        {
            MongoClient dbClient = new MongoClient(<<YOUR ATLAS CONNECTION STRING>>);

            var dbList = dbClient.ListDatabases().ToList();

            Console.WriteLine("The list of databases on this server is: ");
            foreach (var db in dbList)
            {
                Console.WriteLine(db);
            }
        }
    }
}

C# and MongoDB

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. Join me in this Quick Start series and learn how to perform CRUD operations with C# and MongoDB.

Other articles in this Quick Start C# and MongoDB series:


Get Started with MongoDB Atlas

Run MongoDB in the cloud for free with MongoDB Atlas. No credit card required.