Cookies help us deliver our services. By using our services, you agree to our use of cookies. More information

Sort

From NoSQLZoo
Revision as of 17:38, 18 July 2018 by 40166222 (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

sort() is a cursor method that orders the results of a query.
The aggregation framework provides the $sort operator, and mapReduce takes sort as a parameter that is applied to the input documents.

The syntax for sort() is sort({<field>:<value>, <field>:<value>, ...}) where 1 indicates ascending and -1 indicates descending.
The order in which fields are specified dictates which fields are sorted first.

Sort all the documents in world by continent descending, then name ascending. Show only name and continent.
db.world.find({}, {name: 1, continent: 1 ,_id: 0}).sort({"continent": -1, "name": 1});