MONGDB Basic Learning (v)--projection, pagination, sorting, aggregation

Source: Internet
Author: User
Tags mongodb projection mongodb query

MONGDB Basic Learning (v)--projection, pagination, sorting, aggregation

The learning of Basic grammar

The new Year is coming soon, tomorrow is a lover, the day after tomorrow will be a holiday, hope that they can in the holiday before the five and six chapters finish. I'm glad to see that yesterday I wrote "MongoDB for Java" Java Operation MongoDB "can be placed on the front page of the CSDN blog, the content of this chapter is as follows:

(1) The syntax and example learning of MongoDB projection

(2) the limit () and Skip () Methods of MongoDB learn to achieve the effect of paging

(3) MongoDB's sort () method learning

(4) MongoDB aggregation method learning, mainly used to return data records and calculation results

5.1 MongoDB's projection

The MongoDB projection means querying only the necessary data rather than querying the entire data of a file. If a document has 5 fields, you need to display only 3, and then select only 3 fields.

(1) Find () method

MongoDB's Find () method, which is interpreted in the MongoDB query document, accepts the second optional parameter that is the list of fields to retrieve. In MongoDB, when the Find () method is executed, it displays all fields of a document. To limit this, you need to set the field List value 1 or 0. 1 is used to display the field and 0 is used to hide the field.

(2) syntax

The Find () method has a projection basic syntax as follows: DB. Collection_name.find ({},{key:1})

(3) Example

> db.web_app.find ()

{"_id": ObjectId ("54d877e4ebde50baebb6c4c8"), "name": "Chasing The Dreamer", "oldname": "Good fairy Tale", "description": "This is a VR tourism e-commerce website"}

{"_id": ObjectId ("54d87cdfebde50baebb6c4c9"), "name": "Good fairy Tale", "des": "This is an out-of-the-box VR travel Platform"}

{"_id": ObjectId ("54d87cdfebde50baebb6c4ca"), "City": "Xian", "Address": "www.dreamerkr.com.cn"}

> db.web_app.find ({},{"_id": 0, "name": 1})

{"Name": "Chasing The Dreamer"}

{"Name": "Good Fairy Tale"}

{ }

As you can see, only the fields we need to query are shown here. This is often used in the development of API interfaces, we only want to expose some of the data outside, can enhance the security of data.

5.2 MongoDB's Paging

MongoDB's paging mainly uses the limit () and Skip () methods, and their basic operations are as follows:

(1) Limit () method

To limit the records in MongoDB, you need to use the limit () method. The limit () method accepts a numeric parameter, which is the number of documents to be displayed (this MySQL limit is different, when you learn, you can compare learning, think about MySQL or other database operations, how to complete the MongoDB, self-feeling so deepen learning).

(2) Syntax:

The basic syntax for the limit () method is as follows: DB. Collection_name.find (). Limit (number)

(3) Example

>db.web_app.find ()

{"_id": ObjectId ("54d877e4ebde50baebb6c4c8"), "name": "Chasing The Dreamer", "oldname": "Good fairy Tale", "description": "This is a VR tourism e-commerce website"}

{"_id": ObjectId ("54d87cdfebde50baebb6c4c9"), "name": "Good fairy Tale", "des": "This is an out-of-the-box VR travel Platform"}

{"_id": ObjectId ("54d87cdfebde50baebb6c4ca"), "City": "Xian", "Address": "www.dreamerkr.com.cn"}

> db.web_app.find (). Limit (2)

{"_id": ObjectId ("54d877e4ebde50baebb6c4c8"), "name": "Chasing The Dreamer", "oldname": "Good fairy Tale", "description": "This is a VR tourism e-commerce website"}

{"_id": ObjectId ("54d87cdfebde50baebb6c4c9"), "name": "Good fairy Tale", "des": "This is an out-of-the-box VR travel Platform"}

(4) Skip () method

The method Skip () also accepts numeric type parameters and uses the number of skipped documents. For example, skip (5), which means skipping the first five data and showing the data behind it. Note that the default value of the Skip () method is 0, which means that we can call skip without arguments, and MongoDB will give a default parameter of 0.

(5) syntax

The Skip () method has the following basic syntax: DB. Collection_name.find (). Limit (number). Skip (number)

(6) Example

> db.web_app.find ()

{"_id": ObjectId ("54d877e4ebde50baebb6c4c8"), "name": "Chasing The Dreamer", "oldname": "Good fairy Tale", "description": "This is a VR tourism e-commerce website"}

{"_id": ObjectId ("54d87cdfebde50baebb6c4c9"), "name": "Good fairy Tale", "des": "This is an out-of-the-box VR travel Platform"}

{"_id": ObjectId ("54d87cdfebde50baebb6c4ca"), "City": "Xian", "Address": "www.dreamerkr.com.cn"}

> db.web_app.find (). Limit (2). Skip (2)

{"_id": ObjectId ("54d87cdfebde50baebb6c4ca"), "City": "Xian", "Address": "www.dreamerkr.com.cn"}

Here is the paging operation of MongoDB, the page shows two data, just like the mysql {begnum}, {Endnum}, here is replaced by Db.web_app.find (). Limit (Endnum-begnum). Skip (Begnum) Start reading N records from the first few lines.

5.3 MongoDB Sort

(1) Sort () method

To sort documents in MongoDB, you need to use the sort () method. The sort () method accepts a document that contains a list of fields together with their sort order. To specify sort order 1 and-1. 1 is used in ascending order, and-1 for descending order.

(2) syntax

The basic syntax for the sort () method is as follows: DB. Collection_name.find (). Sort ({key:1})

(3) Example

If you do not specify a sort priority, then the sort () method displays the document in ascending order

> db.web_app.find ()

{"_id": ObjectId ("54d877e4ebde50baebb6c4c8"), "name": "Chasing The Dreamer", "oldname": "Good fairy Tale", "description": "This is a VR tourism e-commerce website"}

{"_id": ObjectId ("54d87cdfebde50baebb6c4c9"), "name": "Good fairy Tale", "des": "This is an out-of-the-box VR travel Platform"}

{"_id": ObjectId ("54d87cdfebde50baebb6c4ca"), "City": "Xian", "Address": "www.dreamerkr.com.cn"}

> db.web_app.find (). Sort ({"Name": 1})

{"_id": ObjectId ("54d87cdfebde50baebb6c4ca"), "City": "Xian", "Address": "www.dreamerkr.com.cn"}

{"_id": ObjectId ("54d87cdfebde50baebb6c4c9"), "name": "Good fairy Tale", "des": "This is an out-of-the-box VR travel Platform"}

{"_id": ObjectId ("54d877e4ebde50baebb6c4c8"), "name": "Chasing The Dreamer", "oldname": "Good fairy Tale", "description": "This is a VR tourism e-commerce network

Aggregation of 5.4 MongoDB

Data logging and calculation results are returned during the aggregation operation. Aggregation Operations group values from multiple documents, and can perform various operations, grouping data to return a single result. The SQL COUNT (*) and groupby are equivalent to the aggregation of MongoDB.

(1) Aggregate () method

For aggregation in MongoDB, the aggregate () method should be used.

(2) syntax

The basic syntax for the aggregate () method is as follows: DB. Collection_name.aggregate (aggregate_operation)

(3) Example

> db.web_info.find ()

{"_id": ObjectId ("54d87cdfebde50baebb6c4c9"), "name": "Good fairy Tale", "Address": "This is an out-of-the-box VR travel Platform"}

{"_id": ObjectId ("54db2e0fc50ae3624c1a555c"), "name": "Chasing The Dreamer", "Address": "Http://www.dreamerkr.com"}

{"_id": ObjectId ("54db2e2ac50a6a0d0c6a8c98"), "name": "Chasing The Dreamer", "Address": "Http://www.dreamerkr.com"}

{"_id": ObjectId ("54db2e36c50a607e6eb3d548"), "name": "Chasing The Dreamer", "Address": "Http://www.dreamerkr.com"}

{"_id": ObjectId ("54db2e6bc50a5f841ad5c196"), "name": "Chasing The Dreamer", "Address": "Http://www.dreamerkr.com"}

{"_id": ObjectId ("54db2e6bc50a5f841ad5c197"), "name": "Tom"}

{"_id": ObjectId ("54db2ebdc50a86203da25e72"), "name": "Chasing The Dreamer", "Address": "Http://www.dreamerkr.com"}

{"_id": ObjectId ("54db2ebdc50a86203da25e73"), "name": "Rain Hit"}

{"_id": ObjectId ("54db2ef5c50a536eb77a3d6d"), "name": "Chasing The Dreamer", "Address": "Http://www.dreamerkr.com"}

{"_id": ObjectId ("54db2ef5c50a536eb77a3d6e"), "name": "Rain Hit"}

{"_id": ObjectId ("54DC0687C50ACB624C30DFCB"), "name": "Chasing The Dreamer", "Address": "Http://www.dreamerkr.com"}

{"_id": ObjectId ("54dc0808c50a5945720b1723"), "name": "Chasing The Dreamer", "Address": "Http://www.dreamerkr.com"}

{"_id": ObjectId ("54dc0808c50a5945720b1724"), "name": "Rain Hit 2"}

{"_id": ObjectId ("54dc09adc50ac37356e0f1a9"), "name": "Chasing The Dreamer"}

{"_id": ObjectId ("54dc0a4ac50a4fac80213257"), "name": "Chasing The Dreamer"}

{"_id": ObjectId ("54dc0a8cc50ad7af35250807"), "name": "Dream Chaser 2"}

{"_id": ObjectId ("54DC0AC4C50ADF3258A884BF"), "name": "Dream Chaser 2"}

{"_id": ObjectId ("54dc0baec50ad2416bac179b"), "name": "Chasing The Dreamer"}

{"_id": ObjectId ("54dc0cc3c50afa2987800afe"), "name": "Chasing The Dreamer"}

{"_id": ObjectId ("54dc130fc50a1c2e75e2b05c"), "name": "Chasing The Dreamer", "Address": "Http://www.dreamerkr.com"}

> db.web_info.aggregate ([{$group: {_id: "$name", Num: {$sum: 1}}])

{"_id": null, "num": 2}

{"_id": "Rain Hits 2", "num": 1}

{"_id": "Rain Hit", "num": 3}

{"_id": "Tom", "num": 1}

{"_id": "Chasing The Dreamer", "num": 13}

{"_id": "Dreamer 2", "num": 2}

{"_id": "Good fairy Tale", "num": 3}

The above usage is equivalent to the SQL query select name, COUNT (*) from the Web_info group by name. In the example above, we have grouped the field name document and incremented the sum of the previous values in each number of times name. The list of other aggregation expressions is as follows:

An expression

Describe

Instance

$sum

summarizes the values defined by all the files in the collection .

Db.web_info.aggregate ([{$group: {_id: ' $name ', num: {$sum: ' $address '}}])

$avg

the average calculated from all the given values in all document collections .

Db.web_info.aggregate ([{$group: {_id: ' $name ', num: {$avg: ' $address '}}])

$min

gets the minimum corresponding value in all files in the collection .

Db.web_info.aggregate ([{$group: {_id: ' $name ', num: {$min: ' $address '}}])

$max

gets the maximum of the corresponding value in all the files in the collection .

Db.web_info.aggregate ([{$group: {_id: ' $name ', num: {$max: ' $address '}}])

$push

values are inserted into an array generation document .

Db.web_info.aggregate ([{$group: {_id: ' $name ', address: {$push: ' $address '}}])

$addToSet

value is inserted into an array of the resulting document, but does not create duplicates .

Db.web_info.aggregate ([{$group: {_id: ' $name ', address: {$addToSet: ' $address '}}])

$first

gets the first document from the source document according to the grouping. Typically, this makes sense, along with some previous applications such as "$sort"-stage.

Db.web_info.aggregate ([{$group: {_id: ' $name ', first_address: {$first: ' $address '}}])

$last

gets the final document from the source document according to the grouping. Usually, this makes sense, along with some previous applications such as "$sort"-stage.

Db.web_info.aggregate ([{$group: {_id: ' $name ', last_address: {$last: ' $address '}}])

MONGDB Basic Learning (v)--projection, pagination, sorting, aggregation

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.