Docs Menu
Docs Home
/ / /
Laravel MongoDB
/

Find Multiple Documents

On this page

  • Example

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.

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 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",
...,
},
...
]

This example performs the following actions:

  • Accesses the movies collection by calling the table() method from the DB facade

  • Retrieves 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 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 = 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.

Back

Find a Document

On this page