Docs Menu
Docs Home
/ / /
C#/.NET

Quick Start

On this page

  • Create a MongoDB Cluster
  • Set Up a Free Tier Cluster in Atlas
  • Set Your Connection String
  • Set Up Your Project
  • Create the Project
  • Add MongoDB as a Dependency
  • Query Your MongoDB Cluster from Your Application
  • Next steps

This guide shows you how to create an application that uses the .NET/C# Driver to connect to a MongoDB Atlas cluster. If you prefer to connect to MongoDB using a different driver or programming language, see our list of official MongoDB drivers.

The .NET/C# driver lets you connect to and communicate with MongoDB clusters from a .NET application.

MongoDB Atlas is a fully-managed cloud database service that hosts your data on MongoDB clusters. In this guide, we show you how to get started with your own free (no credit card required) cluster.

Follow the steps below to connect your .NET application with a MongoDB Atlas cluster.

To set up your Atlas Free Tier Cluster required for this guide, complete the guide on MongoDB Atlas Setup.

After completing the steps in the Atlas guide, you have a new MongoDB cluster deployed in Atlas, a new database user, and sample datasets loaded into your cluster. You also have a connection string similar to the following in your copy buffer:

"mongodb+srv://<db_username>:<db_password>@cluster0.abc.mongodb.net/?retryWrites=true&w=majority"

Run the following code at the command prompt to save your MongoDB connection string to an environment variable. This method is safer than including your credentials in your source code.

export MONGODB_URI="<your MongoDB URI>"

Note

PowerShell Environment Variables

If you are using Microsoft PowerShell, run the following command to save your connection string in an environment variable:

set MONGODB_URI="<your MongoDB URI>"

Important

Make sure to replace the <db_username> and <db_password> sections of the connection string with the username and password of your Atlas database user.

For more information about connection strings, see Connection Strings.

Create a new directory and initialize your project with the dotnet new command, as follows:

mkdir csharp-quickstart
cd csharp-quickstart
dotnet new console

Use the dotnet add command to add the .NET/C# Driver to your project as a dependency.

dotnet add package MongoDB.Driver

In this step, you'll use the .NET/C# Driver to connect to your MongoDB cluster and run a query on the sample data. You'll need your preferred text editor or integrated development environment (IDE) installed and running.

Open the file named Program.cs in the base directory of your project. Copy the following sample code into Program.cs

using MongoDB.Driver;
using MongoDB.Bson;
var connectionString = Environment.GetEnvironmentVariable("MONGODB_URI");
if (connectionString == null)
{
Console.WriteLine("You must set your 'MONGODB_URI' environment variable. To learn how to set it, see https://www.mongodb.com/zh-cn/docs/drivers/csharp/current/quick-start/#set-your-connection-string");
Environment.Exit(0);
}
var client = new MongoClient(connectionString);
var collection = client.GetDatabase("sample_mflix").GetCollection<BsonDocument>("movies");
var filter = Builders<BsonDocument>.Filter.Eq("title", "Back to the Future");
var document = collection.Find(filter).First();
Console.WriteLine(document);

This sample code runs a query against your sample dataset in MongoDB Atlas. Run it from your command line by using the following command:

dotnet run csharp-quickstart.csproj

When you run Program.cs, it should output the details of the following movie from the sample dataset:

{
"_id": "573a1398f29313caabce9682",
...
"title": "Back to the Future",
...
}

Tip

If your output is empty, ensure you have loaded the sample datasets into your cluster.

After completing this step, you should have a working application that uses the .NET/C# Driver to connect to your MongoDB cluster, run a query on the sample data, and print out the result.

To learn more about connecting to Atlas with the .NET/C# Driver, see the Atlas driver connection guide and select C# from the Select your language dropdown.

Learn how to read and modify data using the .NET/C# Driver in the CRUD Operations guide or how to perform common operations in Usage Examples.

Back

Previous Versions