MongoDB Language Series
MongoDB: The modern database for C#
Get the versatility and flexibility of MongoDB Atlas with the familiarity of C#. Accelerate development with a first-class developer experience.
Write strongly-typed queries natively
Construct queries using idiomatic builders — no need to learn a new query language. MongoDB supports LINQ operators, so you can get the data you need with the syntax you know.
Manipulate data using the tools of your choice
Easily work with your data using extensions for VS Code and Visual Studio. Browse your databases, prototype queries, and aggregations with dynamic playgrounds, and query data directly from your IDE.
MongoDB Atlas
and C# in action
Connect to MongoDB Atlas with the C# driver
xxxxxxxxxx
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");
Write aggregation pipelines in LINQ
xxxxxxxxxx
// Return largest and smallest cities by state
var pipeline =
from o in
(
from z in __collection.AsQueryable()
group z by new { z.State, z.City } into g
select new { StateAndCity = g.Key, Population = g.Sum(x => x.Population) }
)
orderby o.Population
group o by o.StateAndCity.State into g
orderby g.Key
select new
{
State = g.Key,
BiggestCity = new { Name = g.Last().StateAndCity.City, Population = g.Last().Population },
SmallestCity = new { Name = g.First().StateAndCity.City, Population = g.First().Population }
};
var result = await pipeline.ToListAsync();
Programmatically encrypt key data
xxxxxxxxxx
using MongoDB.Bson;
public static class JsonSchemaCreator
{
private static readonly string DETERMINISTIC_ENCRYPTION_TYPE = "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic";
public static BsonDocument CreateJsonSchema(string keyId)
{
return new BsonDocument
{
{ "bsonType", "object" },
{ "encryptMetadata", CreateEncryptMetadata(keyId) },
{
"properties",
new BsonDocument
{
{
"ssn",
new BsonDocument
{
{
"encrypt",
new BsonDocument
{
{ "bsonType", "int" },
{ "algorithm", DETERMINISTIC_ENCRYPTION_TYPE }
}
}
Boost Developer Productivity with the MongoDB Analyzer for .NET
The MongoDB Analyzer for .NET improves productivity for C# developers by reducing the pain of debugging queries. It quickly identifies when unsupported Builders or LINQ expressions are being used in your applications, enabling you to identify and fix any errors prior to runtime.
Resources for C# developers
Get started with MongoDB for C#
- Serverless backend
- Bidirectional sync
- Intuitive SDK
- Idiomatic builders
- Capable extensions
- Thoughtful tools