MongoDB uses limit and skip to complete paging and cursors (ii)

Source: Internet
Author: User

The $slice operator returns the internal value of the specified array in the document//query out the 2nd to 4th book in The Jim Bookshelf Db.persons.find ({name: "Jim"},{books:{"$slice": [1,3]}})// Query out the last book Db.persons.find ({name: "Jim"},{books:{"$slice": -1},_id:0,name:1})//query out the students who have studied in K//This we can do with an absolute match, but there are some problems ( Looking for a problem? order? always take score?) Db.persons.find ({school:{school: "K", Score: "A"}},{_id:0,school:1})//In order to solve the problem of order I can use the object "." The way to locate Db.persons.find ({"School.score": "A", "School.school": "K"},{_id:0,school:1})//db.persons.find ({" School.score ":" A "," School.school ":" J "},{_id:0,school:1})//Correct practice single condition group query $elemmatchdb.persons.find ({school:{$ Elemmatch:{school: "K", Score: "A"}}) paging and sorting 1. Limit returns the specified number of data bars 1.1 queries out the first 5 data db.persons.find ({},{_id:0,name:1}) in the persons document. Limit (5) 2.Skip returns the span of the specified data 2.1 queries out of the persons document 5 Data Db.persons.find ({},{_id:0,name:1}). Limit (5). Skip (5) 3.Sort returns data sorted by age [1,-1]db.persons.find ({},{_id:0, Name:1,age:1}). Sort ({age:1}) Note: MongoDB key can be stored in different types of data sort and there is also a priority MongoDB key can be stored in different types of data sorting there is also a priority minimum value null numeric string Object/ Document array binary Object ID Boolean date cursor iterates through the query data var persons = Db.persons.find (), while (Persons.hasnext ()) {obj = Persons.next ();p rint ( OBJ.NAME)} cursor severalConditions of destruction 1. The client sends a message that he destroys 2. Cursor iteration completed 3. Default cursors are more than 10 minutes useless and will not be cleared 3. Query snapshot query Snapshot Db.persons.find ({$query: {name: "Jim"}, $snapshot: true}) 

  

Second, pagination and sorting
1.Limit returns the specified number of data bars
1.1 Query out the first 5 data in persons document
Db.persons.find ({},{_id:0,name:1}). Limit (5)
2.Skip returns the span of the specified data
2.1 Querying the data from the 5~10 bar in the persons document
Db.persons.find ({},{_id:0,name:1}). Limit (5). Skip (5)
3.Sort returns data sorted by age [1,-1]
Db.persons.find ({},{_id:0,name:1,age:1}). Sort ({age:1})
Note: MongoDB keys can be sorted with different types of data and also have priority
Minimum value
Null
Digital
String
Objects/Documents
Array
Binary
Object ID
Boolean
Date
Time stamp à regular à maximum value
4.Limit and Skip completing pagination
4.1 Three data bits one page for paging
First page Àdb.persons.find ({},{_id:0,name:1}). Limit (3). Skip (0)
Second page àdb.persons.find ({},{_id:0,name:1}). Limit (3). Skip (3)
4.2skip has a performance problem, no special circumstances we can change the idea
Re-deconstruct the document design

Each time the query operation of the back and forth values to the last document to save the date
Db.persons.find ({date:{$gt: Date Value}}). Limit (3)
Personal suggestion à should put the midpoint of the software on a convenient and accurate query instead of paging performance
Because the user will not be able to search for up to 2 pages
Third, cursors
Using cursors to traverse query data
var persons = Db.persons.find ();
while (Persons.hasnext ()) {
obj = Persons.next ();
Print (Obj.name)
}

2. Cursor several destruction conditions
1. The client sends him a message to destroy it.
2. Cursor Iteration Complete
3. The default cursor is more than 10 minutes and will not be cleared.
3. Querying snapshots
After the snapshot, the cursor movement is made for the invariant collection, and the method of use is seen.
Db.persons.find ({$query: {name: "Jim"}, $snapshot: true})
Advanced query Options
$query
$orderby
$maxsan: The maximum number of documents scanned by integer
$min: Doc Query starts
$max: Doc Query ends
$hint: Which index is used by doc
$explain: Boolean statistics
$snapshot: Boolean Consistent snapshot

MongoDB uses limit and skip to complete paging and cursors (ii)

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.