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
for (i=0;i<15;i++) {Db.t1.insert ({_id:i})}
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