HomeProductsADO.NET Data ProvidersdotConnect for MongoDB: Query MongoDB collections with SQL in your .NET applications

dotConnect for MongoDB: Query MongoDB collections with SQL in your .NET applications

Meet dotConnect for MongoDB, an ADO.NET provider that lets .NET applications work with MongoDB collections through plain SQL and the standard ADO.NET classes.

You can run SELECT queries with JOIN and GROUP BY, change data with INSERT, UPDATE, and DELETE, and rely on Entity Framework Core for object-oriented access. Native MongoDB commands in JSON are there when you need them, and the provider includes the MongoDB client libraries for Windows, so there is nothing else to install.

Querying a MongoDB collection from C# takes just a few lines:

using MongoDBConnection connection = new(
"Host=localhost;" +
"Port=27017;" +
"User Id=your_username;" +
"Password=your_password;" +
"Database=your_database;" +
"License Key=your_license_key");
connection.Open();

using MongoDBCommand command = new("SELECT id, name FROM orders", connection);
using MongoDBDataReader reader = command.ExecuteReader();
while (reader.Read())
Console.WriteLine(reader["name"]);
Download dotConnect for MongoDB

dotConnect for MongoDB: Key features at a glance

Secure connectivity for any MongoDB setup
High performance for data-intensive applications
Rich SQL and data type support for collections
Convenient tooling for everyday .NET development

Secure connectivity for any MongoDB setup

  • Authentication with a separate database. Sign in with a username and password, and specify an authentication database when your users are defined outside the working one.
  • SSL/TLS encryption. Protect connections with a CA certificate and a client certificate.
  • Replica sets and seed lists. Connect to replica sets and additional servers using mongodb:// or mongodb+srv:// (DNS seed list) connection strings.
  • No extra client libraries. The provider has everything it needs, so you only specify the server, database, and credentials.

High performance for data-intensive applications

  • Automatic result batching. Retrieve large query results without writing custom paging logic.
  • Fast bulk inserts. Load rows with the MongoDBLoader component, which sends them to the server in batches of up to 1,000.
MongoDBLoader loader = new("orders", connection);
loader.Open();
for (int i = 1; i <= 100; i++)
{
  loader.SetValue("id", i);
  loader.SetValue("name", "Order " + i);
  loader.SetValue("amount", i * 10.5);
  loader.NextRow();
}
loader.Close();
  • Non-blocking calls. Keep your UI and services responsive using OpenAsync, ExecuteReaderAsync, ExecuteNonQueryAsync, and ExecuteScalarAsync.
  • Pooled connections. Open connections are kept and reused, and you can tune pooling right in the connection string.

Rich SQL and data type support for collections

  • Query collections as tables. Run SELECT (including JOIN and GROUP BY), INSERT, UPDATE, and DELETE statements through the built-in SQL engine.
  • Manage structure with DDL. Use CREATE TABLE with columns and a schema validator, DROP TABLE, CREATE INDEX, CREATE UNIQUE INDEX, and DROP INDEX.
  • Switch to native commands. Turn off the Sql Engine option to send MongoDB commands in JSON.
  • Work with documents and arrays. Nested fields of embedded documents and arrays show up as separate columns, or you can get whole documents and arrays as JSON strings with the Complex As String option.
  • Keep Decimal128 precise. Read Decimal128 values without losing precision, or map them to Decimal or Double when that fits you better.
  • Configure type mapping. Read UUID values as Guid and choose how DateTimeOffset values are stored.
  • Run transactions. BeginTransaction and TransactionScope are available on replica sets and sharded clusters.

Convenient tooling for everyday .NET development

  • Standard ADO.NET classes. MongoDBConnection, MongoDBCommand, MongoDBDataReader, MongoDBDataAdapter, and MongoDBCommandBuilder work the way you’d expect.
  • Beyond the basics. Build connection strings with MongoDBConnectionStringBuilder, get provider instances from MongoDBProviderFactory, retrieve metadata with GetSchema, and use MongoDBScript and MongoDBDump for scripts and for backing up and restoring collections.
  • Disconnected data. MongoDBDataSet and MongoDBDataTable support in-memory processing and data binding.
  • Entity Framework Core. Supports EF Core 3.1 and 8–11, with Database-First and Code-First approaches, migrations, and embedded documents mapped to owned types.
  • Dapper and NHibernate. Both are supported alongside EF Core, so you can keep the data access approach your project already uses.
  • Visual modeling. Create an EF Core model from your MongoDB collections in Entity Developer, adjust it on a diagram, and generate the DbContext and entity classes automatically.
  • Visual Studio integration. In Visual Studio 2017–2026, add MongoDB connections in Server Explorer and set up components with design-time editors and the DataSet Generator.
  • Activity tracing. Follow SQL execution with MongoDBMonitor and dbMonitor, and catch errors through MongoDBException.

Try dotConnect for MongoDB

Want to use MongoDB in your .NET applications through SQL and ADO.NET?

The provider works on Windows (x86 and x64) with .NET 5–11, .NET Core 2.0–3.1, .NET Framework 4.5.1–4.8, and .NET Standard 2.0, and connects to MongoDB 3.6 and later. Linux and macOS support is planned. Download a free 30-day trial to check out SQL querying, bulk loading, and EF Core support in your own project. You can also browse all features and compatibility details or follow the Getting started guide to run your first query.

Download dotConnect for MongoDB

dotConnect Team
dotConnect Teamhttps://www.devart.com/dotconnect/
The dotConnect Team is a group of experienced .NET developers at Devart who specialize in building and supporting dotConnect data providers. They share practical insights, coding tips, and tutorials on .NET development and database connectivity through the Devart blog.
RELATED ARTICLES

Whitepaper

Social

Topics

Products