Docs Menu
Docs Home
/ / /
Laravel MongoDB
/

Update Multiple Documents

On this page

  • Example

You can update multiple documents in a collection by calling the update() method on a query builder.

Pass a query filter to the where() method to retrieve documents that meet a set of criteria. Then, update the matching documents by passing your intended document changes to the update() method.

This usage example performs the following actions:

  • Uses the Movie Eloquent model to represent the movies collection in the sample_mflix database

  • Updates documents from the movies collection that match a query filter

  • Prints the number of updated documents

The example calls the following methods on the Movie model:

  • where(): matches documents in which the value of the imdb.rating nested field is greater than 9.0.

  • update(): updates the matching documents by adding an acclaimed field and setting its value to true. This method returns the number of documents that were successfully updated.

$updates = Movie::where('imdb.rating', '>', 9.0)
->update(['acclaimed' => true]);
echo 'Updated documents: ' . $updates;
Updated documents: 20

To learn how to edit your Laravel application to run the usage example, see the Usage Examples landing page.

Tip

To learn more about updating data with the Laravel Integration, see the Modify Documents section of the Write Operations guide.

Back

Update a Document

On this page