MongoDB (11) MongoDB Sort Document

Source: Internet
Author: User
Sort () method

To sort documents in MongoDB, you need to use the sort () method. The sort () method accepts a document that contains a list of fields together with their sort order. To specify sort order 1 and-1. 1 is used in ascending order, and-1 for descending order.

Grammar:

The basic syntax for the sort () method is as follows

>db. Collection_name.find (). Sort ({key:1})
Example

Consider the collection Myycol with the following data

{"_id": ObjectId (5983548781331ADF45EC5), "title": "MongoDB Overview""_id": ObjectId (5983548781331ADF45EC6) , "title": "NoSQL Overview""_id": ObjectId (5983548781331ADF45EC7), "title": "Yiibai Overview"}

The following example shows a file sorted in descending order by title.

>db.mycol.find ({},{"title": 1,_id:0}). Sort ({"title": -1}) {"title": "Tutorials Point Overview"} { "title": "NoSQL Overview"}{"title": "MongoDB Overview"}>

Note that if sort precedence is not specified, then the sort () method displays the document in ascending order.

MongoDB (11) MongoDB Sort Document

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.