Database Reference
In-Depth Information
However, the following command will not match any documents, even though it might appear identical to the
earlier track list query:
> db.media.find ( { "Tracklist" : {"Track" : "1" }} )
Subobjects must match exactly; therefore, the preceding query would only match a document that contains no
other information, such as Track.Title :
{"Type" : "CD",
"Artist" : "Nirvana"
"Title" : "Nevermind",
"Tracklist" : [
{
"Track" : "1",
},
{
"Track" : "2",
"Title" : "In Bloom",
"Length" : "4:15"
}
]
}
Using the Sort, Limit, and Skip Functions
MongoDB includes several functions that you can use for more precise control over your queries. We'll cover how to
use the sort , limit , and skip functions in this section.
You can use the sort function to sort the results returned from a query. You can sort the results in ascending or
descending order using 1 or -1 , respectively. The function itself is analogous to the ORDER BY statement in SQL, and it
uses the key's name and sorting method as criteria, as in this example:
> db.media.find().sort( { Title: 1 })
This example sorts the results based on the Title key's value in ascending order. This is the default sorting order
when no parameters are specified. You would add the -1 flag to sort in descending order.
Note
if you specify a key for sorting that does not exist, the values will be returned in their ascending insertion order.
You can use the limit() function to specify the maximum number of results returned. This function requires
only one parameter: the number of the desired results returned. When you specify '0, all results will be returned.
The following example returns only the first ten items in your media collection:
> db.media.find().limit( 10 )
Another thing you might want to do is skip the first n documents in a collection. The following example skips the
first twenty documents in your media collection:
> db.media.find().skip( 20 )
 
 
Search WWH ::




Custom Search