Find Multiple Documents
You can retrieve multiple documents from a collection by using a method such as
Model::where()
or methods from the DB
facade to match documents, 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
orderBy()
method.Tip
To learn about other ways to retrieve documents with the Laravel Integration, see the
Read Operations guide.Example
Select from the following
Eloquent and Query Builder tabs to view usage examples for the same operation that use each corresponding query syntax:This 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", ..., }, ... ]
This example performs the following actions:
Accesses the
movies
collection by calling thetable()
method from theDB
facadeRetrieves and prints documents from the
movies
collection that match a query filter
The example calls the following query builder methods:
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 = DB::table('movies') ->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.