Docs Menu
Docs Home
/ / /
Rust Driver
/

Update Multiple Documents

You can update multiple documents in a collection by calling the update_many() method on a Collection instance.

Pass the following parameters to the update_many() method:

  • Query filter, which specifies the criteria to match

  • Update document, which specifies the updates to make to all matching documents

The update_many() method returns an UpdateResult type that contains information about the results of the update operation, such as the number of modified documents.

To learn more about the update_many() method, see the Update Documents section of the Modify Documents guide.

This example updates a document in the restaurants collection of the sample_restaurants database. The update_many() method adds the near_me field to documents in which the value of the address.street field is "Sullivan Street" and the borough field is "Manhattan".

You can access the documents in the restaurants collection as instances of the Document type or a custom data type. To specify which data type represents the collection's data, replace the <T> type parameter on the highlighted line with one of the following values:

  • <Document>: Accesses collection documents as BSON documents.

  • <Restaurant>: Accesses collection documents as instances of the Restaurant struct, defined at the top of the code

Select the Asynchronous or Synchronous tab to see the corresponding code for each runtime:

use std::env;
use mongodb::{ bson::doc, Client, Collection };
use bson::Document;
use serde::{ Deserialize, Serialize };
#[derive(Debug, Serialize, Deserialize)]
struct Address {
street: String,
city: String,
}
#[derive(Serialize, Deserialize, Debug)]
struct Restaurant {
name: String,
borough: String,
address: Address,
}
#[tokio::main]
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 };
#[derive(Debug, Serialize, Deserialize)]
struct Address {
street: String,
city: String,
}
#[derive(Serialize, Deserialize, Debug)]
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

Back

Update One