| CARVIEW |
- MongoDB
- …
- Home
- Developer Zone
- Manual
- Querying
- Sorting
Sorting
|
"Natural order" is defined as the database's native ordering of objects in a collection. When executing a find() with no parameters, the database returns objects in forward natural order. For standard tables, natural order is not particularly useful because, although the order is often close to insertion order, it is not guaranteed to be. However, for Capped Collections, natural order is guaranteed to be the insertion order. This can be very useful. In general, the natural order feature is a very efficient way to store and retrieve data in insertion order (much faster than say, indexing on a timestamp field). But remember, the collection must be capped for this to work. In addition to forward natural order, items may be retrieved in reverse natural order. For example, to return the 50 most recently inserted items (ordered most recent to less recent) from a capped collection, you would invoke: > c=db.cappedCollection.find().sort({$natural:-1}).limit(50)
Sorting can also be done on arbitrary keys in any collection. For example, this sorts by 'name' ascending, then 'age' descending: > c=db.collection.find().sort({name : 1, age : -1})
See Also
|
|
- Added by Rian Murphy, last edited by Dwight Merriman on Aug 26, 2009 (view change)
Labels
- Powered by Atlassian Confluence 3.0.0_01, the Enterprise Wiki.
- Printed by Atlassian Confluence 3.0.0_01, the Enterprise Wiki.
- Bug/feature request –
- Atlassian news –
- Contact administrators

Add Comment