Find Multiple Documents
On this page
You can retrieve multiple documents from a collection by creating a query builder, using a method such
as Model::where()
or the DB
facade to match documents in a collection, and then calling the get()
method to retrieve the results..
Pass a query filter to the where()
method to retrieve documents that meet a
set of criteria. When you call the get()
method, MongoDB returns the
matching documents according to their natural order in the database or
according to the sort order that you can specify by using the orderBy()
method.
To learn more about query builder methods, see the Query Builder guide.
Example
This usage example performs the following actions:
Uses the
Movie
Eloquent model to represent themovies
collection in thesample_mflix
databaseRetrieves and prints documents from the
movies
collection that match a query filter
The example calls the following methods on the Movie
model:
where()
: matches documents in which the value of theruntime
field is greater than900
orderBy()
: sorts matched documents by their ascending_id
valuesget()
: retrieves the query results as a Laravel collection object
$movies = Movie::where('runtime', '>', 900) ->orderBy('_id') ->get();
// Results are truncated [ { "_id": ..., "runtime": 1256, "title": "Centennial", ..., }, { "_id": ..., "runtime": 1140, "title": "Baseball", ..., }, ... ]
To learn how to edit your Laravel application to run the usage example, see the Usage Examples landing page.
Tip
To learn about other ways to retrieve documents with the Laravel Integration, see the Read Operations guide.