MongoDB Basic Tutorial Series--fifth MongoDB mapping and restriction logging

Source: Internet
Author: User

The last mentioned method of find (), the attentive partner will find that the result of the query is to display all the fields in the collection, the actual application, is obviously not enough. So is there a way to specify a specific field to display the document? The answer is yes, MongoDB uses mappings to implement this functionality.

1. Mapping

The display of restricted fields in MongoDB allows you to set the field list with 0 or more. 1 is used to display fields and 0 for hidden fields.

Format

Db. Collection_name.find ({},{key:1})

Example

When you query a document, only the name in the document is displayed. First, all the documents in the user collection are queried, and then the map is used to return the Name field in the document.

> Db.user.find () {"_id": ObjectId ("58e1d2f0bb1bbc3245fa754b"), "name": "Liruihuan", "age": +, "sex": "Man"} {"_id": ObjectId ("58e1d2f0bb1bbc3245fa754d"), "name": "User1", "age": +, "sex": "Man"} {"_id": ObjectId ("58e1d2f0bb1bbc3245fa 754e ")," name ":" User2 "," age ": +," sex ":" Woman "} {" _id ": ObjectId (" 58e1d2f0bb1bbc3245fa754f ")," name ":" User3 "," age ": +," sex ":" Woman "}>db.user.find ({},{" name ": 1," _id ": 0}) {" name ":" Liruihuan "} {" name ":" user1 "} {" name ":" User 2 "} {" name ":" User3 "}>

If you do not add "_id": 0 What results do you return?

> db.user.find ({},{"name": 1}) {"_id": ObjectId ("58e1d2f0bb1bbc3245fa754b"), "name": "Liruihuan"} {"_id": ObjectId (  "58e1d2f0bb1bbc3245fa754d"), "name": "user1"} {"_id": ObjectId ("58e1d2f0bb1bbc3245fa754e"), "name": "User2"} {"_id": ObjectId ("58e1d2f0bb1bbc3245fa754f"), "name": "User3"}>

We found that "_id" is not set: 1 The _id field is also returned in the result because the find() field is always displayed when the method is executed _id . If you do not want to display the field, you can set the "_id": 0.

2. Restricted Records

If you want to display or skip the specified number of documents in MongoDB, you can use the limit () method and the Skip () method

2.1. Limit () method

The limit () method takes a parameter of a numeric type whose value is the number of documents that you want to display.

Format

Db. Collection_name.find (). Limit (number)

Example

When querying a document, only two documents are displayed. All the documents in the user collection are queried first, and then the limit () method is used to display two records.

> Db.user.find () {"_id": ObjectId ("58e1d2f0bb1bbc3245fa754b"), "name": "Liruihuan", "age": +, "sex": "Man"} {"_id": ObjectId ("58e1d2f0bb1bbc3245fa754d"), "name": "User1", "age": +, "sex": "Man"} {"_id": ObjectId ("58e1d2f0bb1bbc3245fa 754e ")," name ":" User2 "," age ": +," sex ":" Woman "} {" _id ": ObjectId (" 58e1d2f0bb1bbc3245fa754f ")," name ":" User3 "," age ":", "Sex": "Woman"}>db.user.find (). Limit (2) {"_id": ObjectId ("58e1d2f0bb1bbc3245fa754b"), "name": "Liruihuan", " Age ":" *, "sex": "Man"} {"_id": ObjectId ("58e1d2f0bb1bbc3245fa754d"), "name": "User1", "age": +, "sex": "Man"}>

If you do not specify a parameter for limit (), we will find that all documents are returned.

> Db.user.find (). Limit () {"_id": ObjectId ("58e1d2f0bb1bbc3245fa754b"), "name": "Liruihuan", "age": +, "sex": "Man"} {"_id": ObjectId ("58e1d2f0bb1bbc3245fa754d"), "name": "User1", "age": +, "sex": "Man"} {"_id": ObjectId ("58e1d2f0bb1bbc3245fa754e"), "name": "User2", "age": +, "sex": "Woman"} {"_id": ObjectId ("58e1d2f0bb1bbc3245fa754f"), "name": "User3", "age": +, "sex": "Woman"}

2.2. Skip () method

The Skip () method takes a parameter of a numeric type whose value is the number of documents that you want to skip.

Format

Db. Collection_name.find (). Limit (number). Skip (number)

Example

When you query a document, only the second document is displayed. First, all the documents in the user collection are queried, then a document is displayed using the limit (1) method, and the first document is skipped using the Skip (1) method.

> Db.user.find () {"_id": ObjectId ("58e1d2f0bb1bbc3245fa754b"), "name": "Liruihuan", "age": +, "sex": "Man"} {"_id": ObjectId ("58e1d2f0bb1bbc3245fa754d"), "name": "User1", "age": +, "sex": "Man"} {"_id": ObjectId ("58e1d2f0bb1bbc3245fa 754e ")," name ":" User2 "," age ": +," sex ":" Woman "} {" _id ": ObjectId (" 58e1d2f0bb1bbc3245fa754f ")," name ":" User3 "," age ": +," sex ":" Woman "}>db.user.find (). Limit (1). Skip (1) {" _id ": ObjectId (" 58e1d2f0bb1bbc3245fa754d ")," name ":" User1 "," age ": +," sex ":" Man "}>

The default value for the Skip () method is 0.

> Db.user.find () {"_id": ObjectId ("58e1d2f0bb1bbc3245fa754b"), "name": "Liruihuan", "age": +, "sex": "Man"} {"_id": ObjectId ("58e1d2f0bb1bbc3245fa754d"), "name": "User1", "age": +, "sex": "Man"} {"_id": ObjectId ("58e1d2f0bb1bbc3245fa 754e ")," name ":" User2 "," age ": +," sex ":" Woman "} {" _id ": ObjectId (" 58e1d2f0bb1bbc3245fa754f ")," name ":" User3 "," age ": +," sex ":" Woman "}> db.user.find (). Skip () {" _id ": ObjectId (" 58e1d2f0bb1bbc3245fa754b ")," name ":" Liruihuan "," AG E ":", "Sex": "Man"} {"_id": ObjectId ("58e1d2f0bb1bbc3245fa754d"), "name": "User1", "age": +, "sex": "Man"} {"_id": O Bjectid ("58e1d2f0bb1bbc3245fa754e"), "name": "User2", "age": +, "sex": "Woman"} {"_id": ObjectId (" 58e1d2f0bb1bbc3245fa754f ")," name ":" User3 "," age ": +," sex ":" Woman "}>

Diligence, desolate and journeys by chance, destroyed by the following.

If you think this article is good or helpful to you, you can give bloggers a little encouragement and support through the "reward" function on the right.

MongoDB Basic Tutorial Series-fifth MongoDB mapping and restriction logging

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.