Limitation
Sort, limit , and offset elements only work in the following cases.
Your query does not have an include element
If you do have an include element, you only specify fields from the primary domain in it.
For example, in the following query, sort, limit and offset will not work because the primary domain is item, but the include element specifies that fields from the artifact, module and build domains should be displayed:
items.find().include("artifact","artifact.module","artifact.module.build")The .limit element restricts the number of records retrieved from the database.
// Find all the jars in artifactory and sort them by repo and name, but only display the first 100 results
items.find({"name" : {"$match":"*.jar"}).sort({"$asc" : ["repo","name"]}).limit(100)Note
Permission filtering occurs after the retrieval limit, so the number of results you see may be lower than the limit you set. For example, if your .limit is set to 10 but you only have permission to view three of those first 10 records, only three are displayed. Other matching records may exist in the database, but they are not shown because the query stopped retrieving data after the first 10.
You can also implement pagination when you want to focus on a subset of your results using the .offset element.
//Run the same example, but this time, display up to 50 items but skipping the first 100
items.find({"name" : {"$match":"*.jar"}).sort({"$asc" : ["repo","name"]}).offset(100).limit(50)