Docs Menu
Docs Home
/ / /
Rust Driver
/

Replace a Document

You can replace a document in a collection by calling the replace_one() method on a Collection instance.

Pass the following parameters to the replace_one() method:

  • Query filter, which specifies the criteria to match

  • Replacement document, which contains the fields and values that will replace the first matched document

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

To learn more about the replace_one() method, see the Replace a Document section of the Modify Documents guide.

This example replaces a document in the restaurants collection of the sample_restaurants database. The replace_one() method replaces the first document in which the value of the name field is "Landmark Coffee Shop" with a new document.

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, perform the following actions on the highlighted lines:

  • To access collection documents as BSON documents, replace the <T> type parameter with <Document> and the <struct or doc> placeholder with replace_doc.

  • To access collection documents as instances of the Restaurant struct, replace the <T> type parameter with <Restaurant> and the <struct or doc> placeholder with replace_struct. The Restaurant struct is defined at the top of the code file.

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

use std::env;
use mongodb::{ bson::doc, Client, Collection };
use serde::{ Deserialize, Serialize };
#[derive(Serialize, Deserialize, Debug)]
struct Restaurant {
borough: String,
cuisine: String,
name: String,
}
#[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! { "name": "Landmark Coffee Shop" };
let replace_doc = doc! {
"borough": "Brooklyn",
"cuisine": "Café/Coffee/Tea",
"name": "Harvest Moon Café",
};
let replace_struct = Restaurant {
borough: "Brooklyn".to_string(),
cuisine: "Café/Coffee/Tea".to_string(),
name: "Harvest Moon Café".to_string(),
};
// Replace <struct or doc> with the replace_struct or replace_doc variable
let res = my_coll.replace_one(filter, <struct or doc>).await?;
println!("Replaced documents: {}", res.modified_count);
Ok(())
}
Replaced documents: 1
use std::env;
use mongodb::{ bson::doc, sync::{ Client, Collection } };
use serde::{ Deserialize, Serialize };
#[derive(Serialize, Deserialize, Debug)]
struct Restaurant {
borough: String,
cuisine: String,
name: String,
}
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! { "name": "Landmark Coffee Shop" };
let replace_doc = doc! {
"borough": "Brooklyn",
"cuisine": "Café/Coffee/Tea",
"name": "Harvest Moon Café",
};
let replace_struct = Restaurant {
borough: "Brooklyn".to_string(),
cuisine: "Café/Coffee/Tea".to_string(),
name: "Harvest Moon Café".to_string(),
};
// Replace <struct or doc> with the replace_struct or replace_doc variable
let res = my_coll.replace_one(filter, <struct or doc>).run()?;
println!("Replaced documents: {}", res.modified_count);
Ok(())
}
Replaced documents: 1

Back

Update Multiple