Docs Menu
Docs Home
/ / /
Laravel MongoDB
/

Find Multiple Documents

On this page

  • Example

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.

This usage example performs the following actions:

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

  • Retrieves 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 the runtime field is greater than 900

  • orderBy(): sorts matched documents by their ascending _id values

  • get(): 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.

Back

Find a Document

On this page