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 uses the
skip()method to set the number of documents to skip and thetake()method to set the total number of documents to returnSort 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.
Before You Get Started
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.phpfile, which contains aMoviemodel to represent documents in themoviescollectionMovieController.phpfile, which contains ashow()function to run database operationsbrowse_movies.blade.phpfile, 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.
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 ]); } }
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.
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:
Natural order in the Server manual glossary
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:
To learn how to retrieve data based on filter criteria, see the Retrieve MongoDB Data guide.