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.