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

Quick Reference

This page shows the driver syntax for several MongoDB commands and links to their related reference and API documentation.

Tip

Using LINQ

You can perform many of the operations on this page using LINQ. For more information on using LINQ with the .NET/C# Driver, see the LINQ Fundamentals page.

Command
Syntax
Find a Document

API Documentation
var filter = Builders<Restaurant>.Filter
.Eq(restaurant => restaurant.Name, "Bagels N Buns");
var restaurant = _restaurantsCollection.Find(filter).FirstOrDefault();
Console.WriteLine(restaurant);
{ name : "Bagels N Buns", restaurant_id : "40363427", ... }
var filter = Builders<Restaurant>.Filter
.Eq(restaurant => restaurant.Name, "Bagels N Buns");
var restaurant = await _restaurantsCollection.Find(filter).FirstOrDefaultAsync();
Console.WriteLine(restaurant);
{ name : "Bagels N Buns", restaurant_id : "40363427", ... }
var filter = Builders<Restaurant>.Filter
.Eq(r => restaurant.Cuisine, "Pizza");
var restaurants = _restaurantsCollection.Find(filter).ToList();
Console.WriteLine(restaurants);
[
{ name: "Como Pizza", cuisine: "Pizza", ... },
{ name: "New York Pizza Suprema", cuisine: "Pizza", ... },
...
]
Find Multiple Documents (Async)

var filter = Builders<Restaurant>.Filter
.Eq(r => restaurant.Cuisine, "Pizza");
var restaurants = await _restaurantsCollection.Find(filter).ToListAsync();
Console.WriteLine(restaurants);
[
{ name: "Como Pizza", cuisine: "Pizza", ... },
{ name: "New York Pizza Suprema", cuisine: "Pizza", ... },
...
]
var insertResult = _restaurantsCollection.InsertOne(new Restaurant { Name = "Mongo's Pizza" });
var insertResult = await _restaurantsCollection.InsertOneAsync(new Restaurant { Name = "Mongo's Pizza" });
_restaurantsCollection.InsertMany(new List<Restaurant>
{
new Restaurant { Name = "Mongo's Pizza" },
new Restaurant { Name = "Mongo Grill" }
});
Insert Multiple Documents (Async)

await _restaurantsCollection.InsertManyAsync(new List<Restaurant>
{
new Restaurant { Name = "Mongo's Pizza" },
new Restaurant { Name = "Mongo Grill" }
});
var filter = Builders<Restaurant>.Filter
.Eq(restaurant => restaurant.Name, "Bagels N Buns");
var update = Builders<Restaurant>.Update
.Set(restaurant => restaurant.Name, "2 Bagels 2 Buns");
var result = _restaurantsCollection.UpdateOne(filter, update);
var filter = Builders<Restaurant>.Filter
.Eq(restaurant => restaurant.Name, "Bagels N Buns");
var update = Builders<Restaurant>.Update
.Set(restaurant => restaurant.Name, "2 Bagels 2 Buns");
var updateResult = await _restaurantsCollection.UpdateOneAsync(filter, update);
var filter = Builders<Restaurant>.Filter
.Eq(restaurant => restaurant.Cuisine, "Pizza");
var update = Builders<Restaurant>.Update
.Set(restaurant => restaurant.Cuisine, "Pasta and breadsticks");
var result = _restaurantsCollection.UpdateMany(filter, update);
Update Multiple Documents (Async)

var filter = Builders<Restaurant>.Filter
.Eq(restaurant => restaurant.Cuisine, "Pizza");
var update = Builders<Restaurant>.Update
.Set(restaurant => restaurant.Cuisine, "Pasta and breadsticks");
var updateResult = await _restaurantsCollection.UpdateManyAsync(filter, update);
Update an Array in a Document

var filter = Builders<Restaurant>.Filter
.Eq(restaurant => restaurant.Name, "Bagels N Buns");
var update = Builders<Restaurant>.Update
.Push(restaurant => restaurant.Grades, new GradeEntry
{
Date = DateTime.Now.ToUniversalTime(),
Grade = "A",
Score = 10
});
var result = _restaurantsCollection.UpdateOne(filter, update);
var filter = Builders<Restaurant>.Filter
.Eq(restaurant => restaurant.Cuisine, "Pizza");
// Find ID of first pizza restaurant
var oldPizzaRestaurant = _restaurantsCollection.Find(filter).First();
var oldId = oldPizzaRestaurant.Id;
Restaurant newPizzaRestaurant = new()
{
Id = oldId,
Name = "Mongo's Pizza",
Cuisine = "Pizza",
Address = new Address
{
Building = "123",
Coordinates = [123, 456],
Street = "Pizza St",
ZipCode = "10003"
},
Borough = "Manhattan",
};
var replaceResult = _restaurantsCollection.ReplaceOne(filter, newPizzaRestaurant);
var filter = Builders<Restaurant>.Filter
.Eq(restaurant => restaurant.Cuisine, "Pizza");
// Find ID of first pizza restaurant
var oldPizzaRestaurant = _restaurantsCollection.Find(filter).First();
var oldId = oldPizzaRestaurant.Id;
Restaurant newPizzaRestaurant = new()
{
Id = oldId,
Name = "Mongo's Pizza",
Cuisine = "Pizza",
Address = new Address
{
Building = "123",
Coordinates = [12.3, 45.6],
Street = "Pizza St",
ZipCode = "10003"
},
Borough = "Manhattan",
};
var replaceResult = await _restaurantsCollection.ReplaceOneAsync(filter, newPizzaRestaurant);
var filter = Builders<Restaurant>.Filter
.Eq(restaurant => restaurant.Name, "Ready Penny Inn");
var deleteResult = _restaurantsCollection.DeleteOne(filter);
var filter = Builders<Restaurant>.Filter
.Eq(restaurant => restaurant.Name, "Ready Penny Inn");
var deleteResult = await _restaurantsCollection.DeleteOneAsync(filter);
var filter = Builders<Restaurant>.Filter
.Regex(restaurant => restaurant.Name, "Green");
var deleteResult = _restaurantsCollection.DeleteMany(filter);
Delete Multiple Documents (Async)

var filter = Builders<Restaurant>.Filter
.Regex(restaurant => restaurant.Name, "Green");
var deleteResult = await _restaurantsCollection.DeleteManyAsync(filter);
Access Data from a Cursor Iteratively

var filter = Builders<Restaurant>.Filter
.Eq(restaurant => restaurant.Cuisine, "Afghan)";
var cursor = _restaurantsCollection.Find(filter).ToCursor();
while (cursor.MoveNext())
{
foreach (var r in cursor.Current)
{
Console.WriteLine(r.Name);
}
}
[
Afghan Kebab House
Khyber Pass
...
]
Count Documents

var filter = Builders<Restaurant>.Filter
.Eq(restaurant => restaurant.Cuisine, "Pizza");
var count = _restaurantsCollection.Find(filter).CountDocuments();
Console.WriteLine(count);
1163
List the Distinct Documents or Field Values

var filter = Builders<Restaurant>.Filter.Empty;
var restaurants = _restaurantsCollection
.Distinct(r => r.Cuisine, filter);
Console.WriteLine(restaurants);
[ Afghan, African, American, Armenian, Asian, ... ]
Limit the Number of Documents Retrieved

var filter = Builders<Restaurant>.Filter
.Eq(restaurant => restaurant.Cuisine, "Asian");
var restaurants = _restaurantsCollection.Find(filter).Limit(3).ToList();
Console.WriteLine(restaurants);
[
{ name: "China Grill", ... },
{ name: "Indo Chine", ... },
{ name: "Le Colonial", ... }
]
Skip Retrieved Documents

var filter = Builders<Restaurant>.Filter
.Eq(restaurant => restaurant.Cuisine, "Asian");
var restaurants = _restaurantsCollection.Find(filter).Skip(2).ToList();
Console.WriteLine(restaurants);
[
{ name: "Le Colonial", ... },
{ name: "Citrus Bar & Grill", ... },
{ name: "Mangez Avec Moi", ... },
...
]
Sort the Documents When Retrieving Them

var filter = Builders<Restaurant>.Filter
.Eq(restaurant => restaurant.Cuisine, "Brazilian");
var restaurants = _restaurantsCollection.Find(filter)
.SortBy(r => r.Name).ToList();
Console.WriteLine(restaurants);
[
{ name: "Barzinho", ... },
{ name: "Beco", ... },
{ name: "Beija-Flor", ... },
...
]
Project Document Fields When Retrieving Them

var filter = Builders<Restaurant>.Filter
.Eq(restaurant => restaurant.Cuisine, "Italian");
var projection = Builders<Restaurant>.Projection
.Include(restaurant => restaurant.Name)
.Include(restaurant => restaurant.Borough)
.Exclude(restaurant => restaurant.Id);
var restaurants = _restaurantsCollection.Find(filter).Project(projection).ToList();
Console.WriteLine(restaurants);
[
{ borough : "Brooklyn", name : "Philadelphia Grille Express" }
{ borough : "Manhattan", name : "Isle Of Capri Restaurant" }
{ borough : "Manhattan", name : "Marchis Restaurant" }
...
]
var index = Builders<Restaurant>.IndexKeys
.Ascending(restaurant => restaurant.Cuisine);
_restaurantsCollection.Indexes
.CreateOne(new CreateIndexModel<Restaurant>(index));

Back

Quick Start

Next

What's New