modify-results

For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt

<!-- Tab options on this page. Append to the .md URL to filter: ?tabs=<id,...> select specific tabs (e.g. ?tabs=nodejs,shell) ?allTabs=true include every tab (no param) default: one tab per tabset

Available tabs: other tabs: query-syntax, controller -->

Modify Query Results

Overview

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](https://www.mongodb.com/docs/drivers/php/laravel-mongodb/current/interact-data/query/modify-results.md#std-label-laravel-skip-limit 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](https://www.mongodb.com/docs/drivers/php/laravel-mongodb/current/interact-data/query/modify-results.md#std-label-laravel-sort 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](https://www.mongodb.com/docs/drivers/php/laravel-mongodb/current/model-data/model-class.md#std-label-laravel-eloquent-model-class section.

Before You Get Started

To run the code examples in this guide, complete the [Quick Start](https://www.mongodb.com/docs/drivers/php/laravel-mongodb/current/get-started.md#std-label-laravel-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.

Skip and Limit Results

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.

Query Syntax

Use the following syntax to specify the query:

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

Sort Query Results

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.

Tip: SortDirection Enum

Starting in Laravel Integration v5.7, you can also specify the sort direction by passing the following SortDirection enum values to the orderBy() method:

  • SortDirection::Ascending for an ascending sort

  • SortDirection::Descending for a descending sort

This enum is available in Laravel 13.8 and later.

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.

Query Syntax

Use the following syntax to specify the query:

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

Tip:

To learn more about sorting, see the following resources:

  • [Natural order](https://www.mongodb.com/docs/manual/reference/glossary/#std-term-natural-order in the Server manual glossary

  • [Ordering, Grouping, Limit, and Offset](https://laravel.com/docs/queries#ordering-grouping-limit-and-offset in the Laravel documentation

Additional Information

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

  • [Find a Document](https://www.mongodb.com/docs/drivers/php/laravel-mongodb/current/interact-data/query/retrieve.md#std-label-laravel-find-one-usage

  • [Find Multiple Documents](https://www.mongodb.com/docs/drivers/php/laravel-mongodb/current/interact-data/query/retrieve.md#std-label-laravel-find-usage

To learn how to retrieve data based on filter criteria, see the [Retrieve MongoDB Data](https://www.mongodb.com/docs/drivers/php/laravel-mongodb/current/interact-data/query/retrieve.md#std-label-laravel-fundamentals-read-retrieve guide.