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

빠른 참조

이 페이지에서는 여러 MongoDB 명령의 드라이버 구문과 관련 참조 및 API 문서에 대한 링크를 보여줍니다.

LINQ 사용

LINQ를 사용하여 이 페이지의 많은 작업을 수행할 수 있습니다. .NET/ C# 드라이버 와 함께 LINQ를 사용하는 방법에 대한 자세한 내용은 LINQ 기본 사항 페이지를 참조하세요.

명령
구문
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);
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));

돌아가기

빠른 시작