Join us Sept 17 at .local NYC! Use code WEB50 to save 50% on tickets. Learn more >
MongoDB Event
Docs Menu
Docs Home
/ / /
C#/ .NET 드라이버
/

문서 업데이트

You can update a single document using the UpdateOne() method on a MongoCollection object. This method requires a query filter, which specifies which document to update, and an update statement, which specifies the changes the driver should make to the first document matching the query filter.

참고

UpdateOne() 메서드는 필터와 일치하는 첫 번째 문서만 업데이트합니다. 두 개 이상의 문서를 업데이트하려면 UpdateMany() 메서드를 사용하세요.

You can pass an instance of UpdateOptions to the UpdateOne() method in order to customize its behavior.

다음 예제에서는 Builders를 사용하여 restaurants 컬렉션에 있는 첫 번째 문서 'Bagels N Buns'의 name을 '2 Bagels 2 Buns'로 업데이트합니다.

Asynchronous 또는 Synchronous 탭을 선택하여 해당 코드를 확인합니다.

// Creates a filter for all documents with a "name" of "Bagels N Buns"
var filter = Builders<Restaurant>.Filter
.Eq(restaurant => restaurant.Name, "Bagels N Buns");
// Creates instructions to update the "name" field of the first document
// that matches the filter
var update = Builders<Restaurant>.Update
.Set(restaurant => restaurant.Name, "2 Bagels 2 Buns");
// Updates the first document that has a "name" value of "Bagels N Buns"
return await _restaurantsCollection.UpdateOneAsync(filter, update);

For a fully runnable example of the UpdateOneAsync() operation, see the UpdateOneAsync Example.

// Creates a filter for all documents with a "name" of "Bagels N Buns"
var filter = Builders<Restaurant>.Filter
.Eq(restaurant => restaurant.Name, "Bagels N Buns");
// Creates instructions to update the "name" field of the first document
// that matches the filter
var update = Builders<Restaurant>.Update
.Set(restaurant => restaurant.Name, "2 Bagels 2 Buns");
// Updates the first document that has a "name" value of "Bagels N Buns"
return _restaurantsCollection.UpdateOne(filter, update);

작업의 완전히 실행 가능한 예를 보려면 UpdateOne UpdateOneAsync() 예제를 참조하세요.

위의 전체 예시 중 하나를 실행한 후 UpdateOne()을 호출할 때마다 콘솔에 다음과 같은 내용이 기록됩니다.

Updated documents: 1

UpdateOne() , UpdateResult 객체를 반환합니다.

문서 업데이트에 대한 자세한 내용은 Update One 가이드를 참조하세요.

빌더 사용에 대해 자세히 알아보려면 빌더를 사용한 작업을 참조하세요.

돌아가기

여러 문서를 삽입합니다.

이 페이지의 내용