MongoDB Four Limit Select skip Skip Sort sort

Source: Internet
Author: User

In relational database selection (limit), sorting (sort) is the same in MongoDB, and it is easier to use.

First, let's take a look at adding a few document in

Now there are four document based on them, to learn the Limit Skip Sort separately

1. Limit Selection

How many are removed from these Document

Make a small example: I only need 2 Document

The result is obvious.

But I still have to explain: limit (2) is to select two document, starting from the entire collection of the first document to select two

What if we don't want to start with the first document?

2.Skip Skip

How many document do I have to skip?

A small example: I'm going to skip the first two document starting with the third document directly

The results are still obvious.

According to international practice to explain: Skip (2) is to skip two document, from the whole collection the first document started jumping, jump back two

Another example: Skip the first one and start with the second one directly.

The problem is, I just want the second and the third how to deal with it?

3.Limit + Skip

The selection from here to There

This is just the problem, a small example: I just want the second and third, how to deal with it?

International practice: Skip the first document select two document from the second article

Another way to do this:

The results are exactly the same for both, but the interpretation of international practice is different.

International practice: Select two document But to skip the first document select from the second article

These two types of writing two select one can

4. Sort sorting

Sort the results by keyword

Take a small example: Find the document in ascending order by price | Descending arrangement

International practice: Ascending according to the Price field, 1 ascending, 1 descending

5. Limit + Skip + Sort Mashup

One example: Select the second third and sort by price in ascending order

The problem arises, shouldn't it be 9800 and then 19800?

Note: the sort + skip + Limit is the priority of their subsectors, respectively, and the second skip last LIMT

Skip + Limit Priority is also the first skip and then limit

Exercises:
Importpymongomongo_client= Pymongo. Mongoclient ('127.0.0.1:27017') DB= mongo_client['Gxy']student_list= [    {"username":"Little Black"," Age": 20,"Gender":"male","Hobby": ["Girl","Glory of Kings"],     "Course": [{"name":"Python","Scour": 60},                {"name":"JavaScript","Scour": 59}]},    {"username":"Small white"," Age": 21,"Gender":"female","Hobby": ["Boy","Glory of Kings"],     "Course": [{"name":"Python","Scour": 80},                {"name":"JavaScript","Scour": 99}]}]#small black changed to small handsome + small white to small bleaching and bleaching#db.python.update ({' username ': ' Little black '},{' $set ': {' username ': ' Little Handsome '}})#db.python.update ({' username ': ' small white '},{' $set ': {' username ': ' Small Bleach '}})#small handsome, small bleaching and bleaching, old age + 5 years old#db.python.update_one ({' username ': ' small Handsome '},{' $inc ': {' Age ': 5}})#db.python.update_one ({' username ': ' Little Bleach '},{' $inc ': {' Age ': 5}})#little Handsome's hobby + chicken eating#db.python.update_one ({' username ': ' Little Handsome '},{' $push ': {' hobby ': ' Eat Chicken '}})#Small Bleach hobby + miracle Warm#db.python.update_one ({' username ': ' small Bleach '},{' $push ': {' hobby ': ' Miracle Warm '}})#small handsome and small bleach join subject HTML score of#db.python.update ({' username ': ' Little Handsome '},{' $push ': {' course ': {' name ': ' HTML ', ' scour ': +}}})#db.python.update ({' username ': ' small Bleach '},{' $push ': {' course ': {' name ': ' HTML ', ' scour ': +}}})#inquire about the sex of the student who likes "girl"#ret= db.python.find_one ({' Hobby ': ' Girls '}) [' Gender ']#print (ret)#find the name of the student who loves "miracle warm"#ret = db.python.find_one ({' Hobby ': ' Girls '}) [' username ']#print (ret)#small handsome and small bleach who have 60 minutes below the courseret= Db.python.find_one ({'Course':[{'Scour': 60}]})Print(ret)#small handsome and small bleach who have 80 + points in the course#small handsome and small bleach who have 60 points below the course and show the course name Score name#small handsome and small bleach who have 80 + courses and show course name Score name#join comment in disciplines: "Excellent" (<60: Failed, >=60 <80: Zhong, >=80 <90: Liang, >=90 excellent)#db.python.remove ({})#Db.python.insert (student_list)res =Db.python.find () forIinchRes:Print(i)
MongoDB Practice

MongoDB Four Limit Select skip Skip Sort sort

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.