For AI agents: a documentation index is available at https://www.mongodb.com/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.
Docs Menu

Modify Query Results

In this guide, you can learn how to customize the way that Laravel MongoDB returns results from queries. You can modify the results of a find operation by chaining more methods to the where() method.

The following sections demonstrate how to modify the behavior of the where() method:

  • Skip and Limit Results uses the skip() method to set the number of documents to skip and the take() method to set the total number of documents to return

  • Sort Query Results uses the orderBy() method to return query results in a specified order based on field values

To learn more about Eloquent models in the Laravel Integration, see the Model Your Data section.

To run the code examples in this guide, complete the Quick Start tutorial. This tutorial provides instructions on setting up a MongoDB Atlas instance with sample data and creating the following files in your Laravel web application:

  • Movie.php file, which contains a Movie model to represent documents in the movies collection

  • MovieController.php file, which contains a show() function to run database operations

  • browse_movies.blade.php file, which contains HTML code to display the results of database operations

The following sections describe how to edit the files in your Laravel application to run the find operation code examples and view the expected output.

This example queries for documents in which the year value is 1999. The operation skips the first 2 matching documents and outputs a total of 3 documents.

Use the following syntax to specify the query:

$movies = Movie::where('year', 1999)
->skip(2)
->take(3)
->get();

To see the query results in the browse_movies view, edit the show() function in the MovieController.php file to resemble the following code:

class MovieController
{
public function show()
{
$movies = Movie::where('year', 1999)
->skip(2)
->take(3)
->get();
return view('browse_movies', [
'movies' => $movies
]);
}
}

To order query results based on the values of specified fields, use the where() method followed by the orderBy() method.

You can set an ascending or descending sort direction on results. By default, the orderBy() method sets an ascending sort on the supplied field name, but you can explicitly specify an ascending sort by passing "asc" as the second parameter. To specify a descending sort, pass "desc" as the second parameter.

If your documents contain duplicate values in a specific field, you can handle the tie by specifying more fields to sort on. This ensures consistent results if the other fields contain unique values.

This example queries for documents in which the value of the countries field contains "Indonesia" and orders results first by an ascending sort on the year field, then a descending sort on the title field.

Use the following syntax to specify the query:

$movies = Movie::where('countries', 'Indonesia')
->orderBy('year')
->orderBy('title', 'desc')
->get();

To see the query results in the browse_movies view, edit the show() function in the MovieController.php file to resemble the following code:

class MovieController
{
public function show()
{
$movies = Movie::where('countries', 'Indonesia')
->orderBy('year')
->orderBy('title', 'desc')
->get();
return view('browse_movies', [
'movies' => $movies
]);
}
}

Tip

To learn more about sorting, see the following resources:

To view runnable code examples that demonstrate how to perform find operations by using the Laravel Integration, see the following usage examples:

To learn how to retrieve data based on filter criteria, see the Retrieve MongoDB Data guide.