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 natural order in the collection or
according to the sort order that you can specify by using the 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
MovieEloquent model to represent themoviescollection in thesample_mflixdatabaseRetrieves and prints documents from the
moviescollection that match a query filter
The example calls the following methods on the Movie model:
where(): Matches documents in which the value of theruntimefield is greater than900orderBy(): Sorts matched documents by their ascending_idvaluesget(): 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
moviescollection by calling thetable()method from theDBfacadeRetrieves and prints documents from the
moviescollection that match a query filter
The example calls the following query builder methods:
where(): Matches documents in which the value of theruntimefield is greater than900orderBy(): Sorts matched documents by their ascending_idvaluesget(): 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.