Video Transcript (English)
Hi! In this video, you will learn how to install the official MongoDB C# driver by using the NuGet package manager.
You’ll also learn how to connect a MongoClient instance in a C# application to an Atlas cluster by using a connection string.
Finally, you’ll learn how to instantiate a single MongoClient that can be reused across all database requests.
Let’s get started by using the NuGet package manager to install the MongoDB driver. To follow along with this demo, open a new C# console app in Visual Studio 2022. I’m using Windows. If you're using a different OS, an IDE, or in a text editor, the experience will be different, but the code will be the same.
To get started with the NuGet package manager, go to the Tools tab at the top of Visual Studio.
From the drop-down menu, click NuGet Package Manager.
Then select Manage NuGet Packages for Solution.
This will open a window that allows us to install and manage our NuGet packages. Let’s search for the MongoDB driver by clicking the Browse tab and typing MongoDB Driver.
The search will bring up a handful of results, but we’re only interested in the one labeled “MongoDB.Driver by MongoDB Inc.” Click that result.
In this window, we’ll select where to install the MongoDB driver. Select the checkbox labeled “Project,” which will add the driver to our solution.
Next, we’ll choose the version of the MongoDB driver that we want.
You should always use the latest stable version available, to ensure you have the newest features.
Now click Install.
We get a prompt showing a list of what’s being installed in our application. Click OK.
Finally, accept the license terms and exit the NuGet tab.
Now that the MongoDB driver is installed, let’s retrieve our connection string to test out connecting with the driver.
We’ll log in to our Atlas account to access our project dashboard.
Once logged in, navigate to the Database tab.
We should see our clusters for our selected project. Next, click the Connect button.
This brings up the following screen.
We want to connect to our application, so select that option.
This brings up another window, where we select the C#/.NET driver.
We can also choose the version that we want to use. For this example, we’ll use the latest version available.
Let’s copy our connection string by selecting this icon.
Now that we’ve retrieved our connection string, let’s use it in our application to test whether the driver was successfully installed.
First, we need to import the MongoDB driver at the top. This will allow us to create our client and interact with our cluster.
Next, we’ll create a variable named mongoURL, and set the value of the MongoURL object to the copied connection string.
Now, we’ll use the mongoURL string to initialize the MongoClient and assign it to a variable named client.
With these 2 lines of code, we now have access to our Atlas cluster via the MongoClient.
We can test this by looping through the databases in our cluster and logging them in the console.
Let’s run and build this solution and observe what happens.
Excellent! It appears that we’re connected and all of our databases have been logged. Well done!
It’s important to note that an application should use a single MongoClient instance for the life of the application to be reused across all database requests. This is because creating MongoClients is resource intensive.
Creating a new MongoClient for each database request will affect the application’s performance.
You now know how to connect to an Atlas cluster from your C# application.
As a recap, here are some takeaways from this video:
We use the NuGet package manager to install the MongoDB C# driver.
We need a valid connection string and the MongoDB C# driver to connect to our Atlas cluster.
The connection string is available in the Atlas UI.
An application should use a single MongoClient instance to access a MongoDB cluster.