Docs Menu

MongoDB C#/.NET Driver

On this page

  • Introduction
  • Take the free online course taught by MongoDB
  • Installation
  • Connect to MongoDB Atlas
  • Stable API
  • Connect to a MongoDB Server on Your Local Machine
  • Compatibility

Welcome to the documentation site for the official MongoDB C#/.NET driver. You can add the driver to your application to work with MongoDB in C#/.NET. Download it using NuGet or set up a runnable project by following our Getting Started guide.

Tip
Use the MongoDB Analyzer

The MongoDB Analyzer is a tool for C#/.NET developers that helps you understand how your code translates into the MongoDB Query API and if your code includes unsupported LINQ or builder expressions.

Banner for the M220N MongoDB University Course
M220N: MongoDB for .NET Developers
Learn the essentials of C# & ASP.NET application development with MongoDB.

NuGet is the simplest way to get the driver. Use MongoDB.Driver for all new projects.

For more information, see Installation.

To connect to a MongoDB Atlas cluster, use the Atlas connection string for your cluster:

using MongoDB.Bson;
using MongoDB.Driver;
// Replace the uri string with your MongoDB deployment's connection string.
var client = new MongoClient(
"mongodb+srv://<username>:<password>@<cluster-address>/test?w=majority"
);
var database = client.GetDatabase("test");
Note

For information about connecting to Atlas Serverless, see the Serverless Instance Limitations page for the minimum driver version you need.

See Connecting for more information.

You can use the Stable API feature starting with MongoDB Server version 5.0 and C#/.NET Driver version 2.13. When you use the Stable API feature, you can update your driver or server without worrying about backward compatibility issues with any commands covered by the Stable API.

Note

Starting from Feburary 2022, the Versioned API is known the Stable API. All concepts and features remain the same with this naming change.

To use this feature, construct a MongoDB client instance, specifying a version of the Stable API:

// Replace <connection string> with your MongoDB deployment's connection string.
var settings = MongoClientSettings.FromConnectionString("<connection string>");
// Set the version of the Stable API on the client.
settings.ServerApi = new ServerApi(ServerApiVersion.V1);
var client = new MongoClient(settings);

See the MongoDB Server reference on the Stable API for more information.

If you need to run a MongoDB server on your local machine for development purposes instead of using an Atlas cluster, you need to complete the following:

  1. Download the Community or Enterprise version of MongoDB Server.
  2. Install and configure MongoDB Server.
  3. Start the server.
Important

Always secure your MongoDB server from malicious attacks. See our Security Checklist for a list of security recommendations.

After you successfully start your MongoDB server, specify your connection string in your driver connection code.

If your MongoDB Server is running locally, you can use the connection string "mongodb://localhost:<port>" where <port> is the port number you configured your server to listen for incoming connections.

If you need to specify a different hostname or IP address, see our Server Manual entry on Connection Strings.

To test whether you can connect to your server, replace the connection string in the Connect to MongoDB Atlas code example and run it.

The following compatibility table specifies the recommended version(s) of the MongoDB C#/.NET driver for use with a specific version of MongoDB.

The first column lists the driver version(s).

C#/.NET Driver Version
MongoDB 6.0
MongoDB 5.0
MongoDB 4.4
MongoDB 4.2
MongoDB 4.0
MongoDB 3.6
MongoDB 3.4
MongoDB 3.2
MongoDB 3.0
MongoDB 2.6
2.16
2.15
2.14
2.13
2.12
2.11
2.10
2.9
2.8
2.7
2.6
2.5
2.4
2.3
2.2
2.0
1.11
1.10

The driver does not support older versions of MongoDB.

The following compatibility table specifies the recommended version(s) of the MongoDB C#/.NET driver for use with a specific version of .NET.

The first column lists the driver version(s).

C#/.NET Driver Version
.NET 5/6 [1]
.NET Core 3.1
.NET Core 3.0
.NET Core 2.2
.NET Core 2.1
.NET Core 2.0
.NET Core 1.1
.NET Core 1.0
.NET 4.8
.NET 4.7 [2]
.NET 4.6
.NET 4.5 [3]
2.16

2.15

2.14

2.13

2.12

2.11

2.10

2.9

2.8

2.7

2.6

2.5

2.4

2.3

2.2

2.0

Versions 1.10 and 1.11 of the driver are compatible with .NET 3.5 through 4.8 only.

[1] When using .NET 5/6, you can't connect to Atlas clusters running MongoDB 4.0 due to a certificate issue. This does not impact clusters running MongoDB 4.2+.
[2] .NET/C# driver versions 2.14 or later requires .NET Framework 4.7.2 or later.
[3] .NET/C# driver versions 2.8 to 2.13 requires .NET Framework 4.5.2 or later.

For more information on how to read the compatibility tables, see our guide on MongoDB Compatibility Tables.

If your Visual Studio IDE project depends on the official Mongo C# driver version 2.10 or later, you must use Visual Studio 2017 or later.

←  MongoDB C++ DriverMongoDB Java Drivers →
Give Feedback
© 2022 MongoDB, Inc.

About

  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2022 MongoDB, Inc.