ANNOUNCEMENT: Voyage AI joins MongoDB to power more accurate and trustworthy AI applications on Atlas.
Learn more
Docs Menu

문서 삽입

동기 InsertOne() 메서드 또는 비동기 InsertOneAsync() 메서드를 사용하여 컬렉션에 단일 문서를 삽입할 수 있습니다.

다음 예는 restaurants 컬렉션에 문서를 삽입하는 예시입니다.

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

// Generates a new restaurant document
Restaurant newRestaurant = new()
{
Name = "Mongo's Pizza",
RestaurantId = "12345",
Cuisine = "Pizza",
Address = new()
{
Street = "Pizza St",
ZipCode = "10003"
},
Borough = "Manhattan",
};
// Asynchronously inserts the new document into the restaurants collection
await _restaurantsCollection.InsertOneAsync(newRestaurant);

InsertOneAsync() 작업의 완전히 실행 가능한 예시는 비동기식 삽입 예시를 참조하세요.

// Generates a new restaurant document
Restaurant newRestaurant = new()
{
Name = "Mongo's Pizza",
RestaurantId = "12345",
Cuisine = "Pizza",
Address = new()
{
Street = "Pizza St",
ZipCode = "10003"
},
Borough = "Manhattan",
};
// Inserts the new document into the restaurants collection
_restaurantsCollection.InsertOne(newRestaurant);

InsertOne() 작업의 완전히 실행 가능한 예시는 동기 삽입 예시를 참조하세요.

위의 전체 예시 중 하나를 실행하면 InsertOne() 메서드는 문서를 삽입하고 Find () 메서드는 새로 삽입한 문서를 반환합니다. 다음과 유사한 출력이 표시됩니다.

Inserting a document...
Document Inserted: { "_id" : ObjectId("..."), "name" : "Mongo's Pizza", "restaurant_id" : "12345", "cuisine" : "Pizza", "address" : { "_t" : "MongoDB.Bson.BsonDocument, MongoDB.Bson", "_v" : { "street" : "Pizza St", "zipcode" : "10003" } }, "borough" : "Manhattan", "grades" : [{ "_t" : "MongoDB.Bson.BsonDocument, MongoDB.Bson", "_v" : { } }] }

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