From 4ca05fe13841ad1d541aa4010127b03016deb64b Mon Sep 17 00:00:00 2001 From: Tyler Brock Date: Tue, 10 Apr 2012 23:10:43 -0400 Subject: [PATCH] minor: added section on sorting to tutorial --- docs/TUTORIAL.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/docs/TUTORIAL.md b/docs/TUTORIAL.md index e40d443..cd09e65 100644 --- a/docs/TUTORIAL.md +++ b/docs/TUTORIAL.md @@ -170,6 +170,18 @@ and it should just print just one document: {"_id"=>BSON::ObjectId('4f7b20b4e4d30b35c9000049'), "i"=>71} +#### Sorting Documents in a Collection + +To sort documents, simply use the `sort` method. The method can either take a key or an array of [key, direction] pairs to sort by. + +Direction defaults to ascending order but can be specified as Mongo::ASCENDING, :ascending, or :asc whereas descending order can be specified with Mongo::DESCENDING, :descending, or :desc. + + # Sort in ascending order by :i + coll.find.sort(:i) + + # Sort in descending order by :i + coll.find.sort([:i, :desc]) + #### Counting Documents in a Collection Now that we've inserted 101 documents (the 100 we did in the loop, plus the first one), we can check to see if we have them all using the `count` method.