Collection 인스턴스 에서 update_many() 메서드를 호출하여 컬렉션 의 여러 문서를 업데이트 할 수 있습니다.
다음 매개변수를 update_many() 메서드에 전달합니다:
- 일치시킬 기준을 지정하는 쿼리 필터 
- 일치하는 모든 문서에 수행할 업데이트를 지정하는 업데이트 문서 
update_many() 메서드는 수정된 문서 수와 같은 업데이트 작업 결과에 대한 정보가 포함된 UpdateResult 유형을 반환합니다.
update_many() 메서드에 학습 보려면 문서 수정 가이드 의 문서 업데이트 섹션을 참조하세요.
예시
이 예시 sample_restaurants 데이터베이스 의 restaurants 컬렉션 에 있는 문서 업데이트합니다. update_many() 메서드는 address.street 필드 의 값이 "Sullivan Street" 이고 borough 필드 의 값이 "Manhattan"인 문서에 near_me 필드 추가합니다.
restaurants 컬렉션 의 문서에 Document 유형 또는 사용자 지정 데이터 유형 의 인스턴스로 액세스 할 수 있습니다. 컬렉션의 데이터를 나타내는 데이터 유형 지정하려면 강조 표시된 줄의 <T> 유형 매개변수를 다음 값 중 하나로 바꿉니다.
- <Document>: 컬렉션 문서를 BSON 문서로 액세스합니다.
- <Restaurant>: 코드 상단에 정의된- Restaurant구조체의 인스턴스로 컬렉션 문서에 액세스합니다.
Asynchronous 또는 Synchronous 탭을 선택하여 각 런타임에 해당하는 코드를 확인합니다.
use std::env; use mongodb::{ bson::doc, Client, Collection }; use bson::Document; use serde::{ Deserialize, Serialize }; struct Address {     street: String,     city: String, } struct Restaurant {     name: String,     borough: String,     address: Address, } async fn main() -> mongodb::error::Result<()> {     let uri = "<connection string>";     let client = Client::with_uri_str(uri).await?;     // Replace <T> with the <Document> or <Restaurant> type parameter     let my_coll: Collection<T> = client         .database("sample_restaurants")         .collection("restaurants");     let filter =         doc! {              "address.street": "Sullivan Street",              "borough": "Manhattan"          };     let update = doc! { "$set": doc! { "near_me": true } };     let res = my_coll.update_many(filter, update).await?;     println!("Updated documents: {}", res.modified_count);     Ok(()) } 
// Your values might differ Updated documents: 22 
use std::env; use mongodb::{     bson::{ Document, doc },     sync::{ Client, Collection }  }; use serde::{ Deserialize, Serialize }; struct Address {     street: String,     city: String, } struct Restaurant {     name: String,     borough: String,     address: Address, } fn main() -> mongodb::error::Result<()> {     let uri = "<connection string>";     let client = Client::with_uri_str(uri)?;     // Replace <T> with the <Document> or <Restaurant> type parameter     let my_coll: Collection<T> = client         .database("sample_restaurants")         .collection("restaurants");     let filter =         doc! {          "address.street": "Sullivan Street",          "borough": "Manhattan"      };     let update = doc! { "$set": doc! { "near_me": true } };     let res = my_coll.update_many(filter, update).run()?;     println!("Updated documents: {}", res.modified_count);     Ok(()) } 
// Your values might differ Updated documents: 22