You can update a document in a collection by retrieving a single document and calling the update()
method on an Eloquent model.
Pass a query filter to the where()
method, sort the matching documents, and call the first()
method to retrieve only the first document. Then, update this matching document by passing your intended document changes to the update()
method.
To learn more about updating data with the Laravel Integration, see the Modify Documents section of the Write Operations guide.
This example performs the following actions:
Uses the Movie
Eloquent model to represent the movies
collection in the sample_mflix
database
Updates a document from the movies
collection that matches the 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 title
field is "Carol"
orderBy()
: Sorts matched documents by their ascending _id
values
first()
: Retrieves only the first matching document
update()
: Updates the value of the imdb.rating
nested field to from 6.9
to 7.3
and the value of the imdb.votes
nested field from 493
to 142000
$updates = Movie::where('title', 'Carol') ->orderBy('id') ->first() ->update([ 'imdb' => [ 'rating' => 7.3, 'votes' => 142000, ], ]);echo 'Updated documents: ' . $updates;
To learn how to edit your Laravel application to run the usage example, see the Usage Examples landing page.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4