문서 메뉴
문서 홈
/ / /
Rust 드라이버
/

문서 업데이트

update_one() Collection 인스턴스에서 메서드를 호출합니다.

다음 매개변수를 update_one() 메서드에 전달합니다:

  • 일치시킬 기준을 지정하는 쿼리 필터

  • 업데이트 문서: 일치하는 첫 번째 문서에 수행할 업데이트를 지정합니다.

update_one() 메서드는 UpdateResult 를 반환합니다. 수정된 문서 수와 같은 업데이트 작업 결과에 대한 정보가 포함된 유형입니다.

update_one() 메서드에 대해 자세히 알아보려면 문서 수정 가이드의 문서 업데이트 섹션을 참조하세요.

이 예에서는 sample_restaurants 데이터베이스의 restaurants collection에 있는 문서를 업데이트합니다.

다음 코드는 name 필드의 값이 "Spice Market" 인 문서에 price 필드를 추가합니다. MongoDB는 쿼리 필터와 일치하는 첫 번째 문서를 업데이트합니다.

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

use std::env;
use mongodb::{
bson::{ Document, doc },
Client,
Collection
};
#[tokio::main]
async fn main() -> mongodb::error::Result<()> {
let uri = "<connection string>";
let client = Client::with_uri_str(uri).await?;
let my_coll: Collection<Document> = client
.database("sample_restaurants")
.collection("restaurants");
let filter = doc! { "name": "Spice Market" };
let update = doc! { "$set": doc! {"price": "$$$"} };
let res = my_coll.update_one(filter, update).await?;
println!("Updated documents: {}", res.modified_count);
Ok(())
}
use std::env;
use mongodb::{
bson::{ Document, doc },
sync::{ Client, Collection }
};
fn main() -> mongodb::error::Result<()> {
let uri = "<connection string>";
let client = Client::with_uri_str(uri)?;
let my_coll: Collection<Document> = client
.database("sample_restaurants")
.collection("restaurants");
let filter = doc! { "name": "Spice Market" };
let update = doc! { "$set": doc! {"price": "$$$"} };
let res = my_coll.update_one(filter, update).run()?;
println!("Updated documents: {}", res.modified_count);
Ok(())
}

돌아가기

여러 문서를 삽입합니다.