Docs Menu

MongoDB C#/.NET Driver

On this page

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

The official MongoDB C#/.NET Driver provides asynchronous interaction with MongoDB.

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 Versioned API feature starting with MongoDB Server version 5.0 and C#/.NET Driver version 2.13. When you use the Versioned API feature, you can update your driver or server without worrying about backward compatibility issues with any commands covered by the Versioned API. To use this feature, construct a MongoDB client instance, specifying a version of the Versioned API:

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

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.

C#/.NET Driver Version
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.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.

C#/.NET Driver Version
.NET 5 [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
.NET 4.6
.NET 4.5
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, you can't connect to Atlas clusters running MongoDB 4.0 due to a certificate issue.

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.

Give Feedback
© 2021 MongoDB, Inc.

About

  • Careers
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2021 MongoDB, Inc.