Mongodbsortlimit and skip usage

Source: Internet
Author: User
To retrieve data, you can use the sort () method to sort the data, specify the sorting field, and use 1 or-1 to specify whether the sorting method is ascending or descending. Similar to the orderby statement in an SQL statement. You can use the limit () method to read a specified amount of data, or the skip () method to skip a specified amount of data. The paging performance is very efficient. 1.

To retrieve data, you can use the sort () method to sort the data, specify the sorting field, and use 1 or-1 to specify whether the sorting method is ascending or descending. Similar to the order by statement in an SQL statement. You can use the limit () method to read a specified amount of data, or the skip () method to skip a specified amount of data. The paging performance is very efficient. 1.

To retrieve data, you can use the sort () method to sort the data, specify the sorting field, and use 1 or-1 to specify whether the sorting method is ascending or descending. Similar to the order by statement in an SQL statement. You can use the limit () method to read a specified amount of data, or the skip () method to skip a specified amount of data. The paging performance is very efficient. 1. syntax> db. COLLECTION_NAME.find (). sort ({KEY: 1})> db. COLLECTION_NAME.find (). limit (NUMBER)> db. COLLECTION_NAME.find (). limit (NUMBER ). skip (NUMBER) 2. sort ()
> db.mediaCollection.find().sort({"Tracklist":1}).toArray()[        {                "_id" : ObjectId("5353462f93efef02c962da71"),                "Type" : "Book",                "Title" : "Definitive Guide to MongoDB, the",                "ISBN" : "987-1-4302-3051-9",                "Publisher" : "Apress",                "Author" : [                        "Membrey, Peter",                        "Plugge, Eelco",                        "Hawkins, Tim"                ]        },        {                "_id" : ObjectId("5353462f93efef02c962da72"),                "Type" : "CD",                "Artist" : "Nirvana",                "Title" : "Nevermind"        },        {                "_id" : ObjectId("5353463193efef02c962da73"),                "Type" : "CD",                "Artist" : "Nirvana",                "Title" : "Nevermind",                "Tracklist" : [                        {                                "Track" : "1",                                "Title" : "Smells like teen spirit",                                "Length" : "5:02"                        },                        {                                "Track" : "2",                                "Title" : "In Bloom",                                "Length" : "4:15"                        }                ]        }]> db.mediaCollection.find().toArray()[        {                "_id" : ObjectId("5353462f93efef02c962da71"),                "Type" : "Book",                "Title" : "Definitive Guide to MongoDB, the",                "ISBN" : "987-1-4302-3051-9",                "Publisher" : "Apress",                "Author" : [                        "Membrey, Peter",                        "Plugge, Eelco",                        "Hawkins, Tim"                ]        },        {                "_id" : ObjectId("5353462f93efef02c962da72"),                "Type" : "CD",                "Artist" : "Nirvana",                "Title" : "Nevermind"        },        {                "_id" : ObjectId("5353463193efef02c962da73"),                "Type" : "CD",                "Artist" : "Nirvana",                "Title" : "Nevermind",                "Tracklist" : [                        {                                "Track" : "1",                                "Title" : "Smells like teen spirit",                                "Length" : "5:02"                        },                        {                                "Track" : "2",                                "Title" : "In Bloom",                                "Length" : "4:15"                        }                ]        }]
Note: If the specified sort key does not exist, the values are returned in ascending order of insertion. 3. limit () This function is used to specify the maximum number of returned results.
> db.mediaCollection.find().limit(1).toArray()[        {                "_id" : ObjectId("5353462f93efef02c962da71"),                "Type" : "Book",                "Title" : "Definitive Guide to MongoDB, the",                "ISBN" : "987-1-4302-3051-9",                "Publisher" : "Apress",                "Author" : [                        "Membrey, Peter",                        "Plugge, Eelco",                        "Hawkins, Tim"                ]        }]
4. skip () skips the first two pieces of data.
> db.mediaCollection.find().skip(2).toArray()[        {                "_id" : ObjectId("5353463193efef02c962da73"),                "Type" : "CD",                "Artist" : "Nirvana",                "Title" : "Nevermind",                "Tracklist" : [                        {                                "Track" : "1",                                "Title" : "Smells like teen spirit",                                "Length" : "5:02"                        },                        {                                "Track" : "2",                                "Title" : "In Bloom",                                "Length" : "4:15"                        }                ]        }]
5. Combined Use
> db.mediaCollection.find().sort({"Tracklist":1}).toArray()[        {                "_id" : ObjectId("5353462f93efef02c962da71"),                "Type" : "Book",                "Title" : "Definitive Guide to MongoDB, the",                "ISBN" : "987-1-4302-3051-9",                "Publisher" : "Apress",                "Author" : [                        "Membrey, Peter",                        "Plugge, Eelco",                        "Hawkins, Tim"                ]        },        {                "_id" : ObjectId("5353462f93efef02c962da72"),                "Type" : "CD",                "Artist" : "Nirvana",                "Title" : "Nevermind"        },        {                "_id" : ObjectId("5353463193efef02c962da73"),                "Type" : "CD",                "Artist" : "Nirvana",                "Title" : "Nevermind",                "Tracklist" : [                        {                                "Track" : "1",                                "Title" : "Smells like teen spirit",                                "Length" : "5:02"                        },                        {                                "Track" : "2",                                "Title" : "In Bloom",                                "Length" : "4:15"                        }                ]        }]> db.mediaCollection.find().sort({"Tracklist":1}).limit(1).skip(1).toArray()[        {                "_id" : ObjectId("5353462f93efef02c962da72"),                "Type" : "CD",                "Artist" : "Nirvana",                "Title" : "Nevermind"        }]
6. natural order $ natural
> db.mediaCollection.find().sort( { $natural: -1 } ).toArray()[        {                "_id" : ObjectId("5353463193efef02c962da73"),                "Type" : "CD",                "Artist" : "Nirvana",                "Title" : "Nevermind",                "Tracklist" : [                        {                                "Track" : "1",                                "Title" : "Smells like teen spirit",                                "Length" : "5:02"                        },                        {                                "Track" : "2",                                "Title" : "In Bloom",                                "Length" : "4:15"                        }                ]        },        {                "_id" : ObjectId("5353462f93efef02c962da72"),                "Type" : "CD",                "Artist" : "Nirvana",                "Title" : "Nevermind"        },        {                "_id" : ObjectId("5353462f93efef02c962da71"),                "Type" : "Book",                "Title" : "Definitive Guide to MongoDB, the",                "ISBN" : "987-1-4302-3051-9",                "Publisher" : "Apress",                "Author" : [                        "Membrey, Peter",                        "Plugge, Eelco",                        "Hawkins, Tim"                ]        }]

Original article address: mongodb sort limit and skip usage. Thank you for sharing it with me.

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.