4.Mongodb Data Query 2

Source: Internet
Author: User
Tags mongodb

1.limit &skip (1) limit
    • Method Limit (): Used to read a specified number of documents
    • Grammar:
Db. Collection name. Find (). Limit (number)

  

    • The number of the parameter indicates how many bars to get the document
    • Displays all documents in the collection if no parameters are specified
    • Example 1: Query 2 Student Information
Db.stu.find (). Limit (2)

  

(2) Skip
    • Method Skip (): Used to skip a specified number of documents
    • Grammar:
Db. Collection name. Find (). Skip (number)

  

    • The number of the parameter indicates the count of skipped records, the default value is 0
    • Example 2: Querying student information starting with 3rd
Db.stu.find (). Skip (2)

  

(3) Use together
    • Method Limit () and skip () can be used together, in order of precedence

    • Create a data set

for (i=0;i<15;i++) {Db.t1.insert ({_id:i})}

  

    • Query 5th to 8th data
Db.stu.find (). Limit (4). Skip (5) or Db.stu.find (). Skip (5). Limit (4)

  

2. Projection
    • In the returned results of the query, select only the necessary fields instead of selecting the entire field of a document
    • such as: A document has 5 fields, need to display only 3, projected 3 of the fields can be
    • Grammar:
    • parameter is field and value, value 1 indicates display, value 0 does not display
Db. Collection name. Find ({},{field name: 1,...})

  

    • For fields that need to be displayed, set to 1, not set to not display
    • Special: For _id columns default is displayed, if not displayed need to explicitly set to 0

Example 1

Db.stu.find ({},{name:1,gender:1})

  

Example 2

Db.stu.find ({},{_id:0,name:1,gender:1})

  

3. Sorting
    • Method sort (), which is used to sort the result set
    • Grammar
Db. Collection name. Find (). Sort ({field: 1,...})

  

    • Parameter 1 is in ascending order
    • Parameter-1 is in descending order
    • Example 1: Descending according to sex, and then ascending according to age
Db.stu.find (). Sort ({gender:-1,age:1})

  

4. Number of statistics
    • Method count () is used to count the number of document bars in the result set
    • Grammar
Db. Collection name. Find ({condition}). Count ()
    • can also be associated with a
Db. Collection name. Count ({condition})

  

    • Example 1: Statistics on the number of males
Db.stu.find ({gender:1}). Count ()

  

    • Example 2: Number of males with a statistical age greater than 20
Db.stu.count ({age:{$gt: 20},gender:1})

  

5. Eliminate duplication
    • Method Distinct () to de-weight the data
    • Grammar
Db. Collection name. Distinct (' Redo field ', {condition})

  

    • Example 1: Finding gender older than 18 (de-weight)
Db.stu.distinct (' Gender ', {age:{$gt: 18}})

  

  

4.Mongodb Data Query 2

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.