|
"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
|

IF YOU HAVE A QUESTION, POST IT TO THE USER GROUP.
These pages are fine for comments, but for questions, your best bet will always be the MongoDB User Group. blog comments powered by Disqus