Join us at MongoDB.local London on 7 May to unlock new possibilities for your data. Use WEB50 to save 50%.
Register now >
Docs Menu
Docs Home
/ /

Update Arrays in a Document

In this guide, you can learn how to use the Rust driver to update array fields in MongoDB documents.

To update an array in a document, you can use an update operator and a positional operator. Update operators specify the type of update to perform, and positional operators specify which array element to update.

The examples in this guide use the following sample documents in the students collection:

{ "_id": 1, "student": "Kai Ling", "test_scores": [88, 62, 73] },
{ "_id": 2, "student": "Francesca Miao", "test_scores": [95, 45, 67] }

Use the positional operator $ to update the first array element that matches your query filter. The $ operator represents the position of the first matching element in the array.

The following example uses the update_one() method to find a document in which the test_scores array contains the value 62. The example then uses the positional operator to update the first matching element from 62 to 65:

let filter = doc! { "test_scores": 62 };
let update = doc! { "$set": { "test_scores.$": 65 } };
let res = my_coll
.update_one(filter, update)
.await?;
println!("Modified documents: {}", res.modified_count);
Modified documents: 1

The following document reflects the changes resulting from the preceding update operation:

{ "_id": 1, "student": "Kai Ling", "test_scores": [88, 65, 73] }

Use the all positional operator $[] to update all elements in an array field. The $[] operator indicates that the update applies to every element in the array.

The following example uses the update_one() method to find the document in which the student field value is "Kai Ling". The example then uses the all positional operator to increment every value in the test_scores array by 5:

let filter = doc! { "student": "Kai Ling" };
let update = doc! { "$inc": { "test_scores.$[]": 5 } };
let res = my_coll
.update_one(filter, update)
.await?;
println!("Modified documents: {}", res.modified_count);
Modified documents: 1

The preceding update operation results in a document similar to the following:

{ "_id": 1, "student": "Kai Ling", "test_scores": [93, 67, 78] }

Use the filtered positional operator $[<identifier>] to update all array elements that match an array filter. An array filter is a document that specifies which array elements to update. Set the <identifier> to a placeholder name that you then reference in the array filter.

To use the filtered positional operator, chain the array_filters() method to your update method call and pass in an array of filter documents.

The following example uses the update_many() method to find all documents in the students collection. The example then uses the filtered positional operator to add 8 points to all test_scores values that are less than 70:

let filter = doc! {};
let update = doc! { "$inc": { "test_scores.$[score]": 8 } };
let res = my_coll
.update_many(filter, update)
.array_filters(vec![doc! { "score": { "$lt": 70 } }])
.await?;
println!("Modified documents: {}", res.modified_count);
Modified documents: 2

The preceding update operation results in documents similar to the following:

{ "_id": 1, "student": "Kai Ling", "test_scores": [88, 70, 73] },
{ "_id": 2, "student": "Francesca Miao", "test_scores": [95, 53, 75] }

For more information about the concepts in this guide, see the following documentation:

  • Update Documents guide

  • Specify a Query guide

To learn more about the operators mentioned in this guide, see the following MongoDB Server manual documentation:

  • $ (update operator)

  • $[] (update operator)

  • $[<identifier>] (update operator)

  • Array Update Operators

To learn more about the methods and types mentioned in this guide, see the following API documentation:

Back

Replace Documents

On this page