MongDB basic learning (5) -- projection, paging, sorting, aggregation, and mongdb Paging
MongDB basic learning (5) -- projection, paging, sorting, and aggregation
Basic syntax Learning
The Chinese New Year is coming soon, tomorrow is a lover, and the day after tomorrow is coming soon. I hope I can finish chapter 5 and chapter 6 before the holiday. I am very happy to see that the "[MongoDB for Java] Java MongoDB operations" I wrote yesterday can be placed on the headlines of the CSDN blog homepage. The content of this chapter is as follows:
(1) MongoDB projection syntax and example Learning
(2) the Limit () and Skip () Methods of MongoDB are learned to achieve paging effect.
(3) MongoDB sort () method learning
(4) MongoDB aggregation method learning, mainly used to return data records and computing results
5.1 MongoDB projection
Mongodb projection means to query only the necessary data, rather than all the data of a file. If a document has five fields, only three fields need to be displayed, and only three fields are selected.
(1) find () method
The find () method of MongoDB. In the MongoDB query documentation, the second optional parameter is the list of fields to be retrieved. In MongoDB, when the find () method is executed, all fields in a document are displayed. To limit this, you need to set the Field List Value 1 or 0. 1 is used to display fields, and 0 is used to hide fields.
(2) syntax
The basic syntax of the find () method for projection is as follows: db. COLLECTION_NAME.find ({}, {KEY: 1 })
(3) Example
>Db. web_app.find ()
{"_ Id": ObjectId ("54d877e4ebde50baebb6c4c8"), "name": "Dream Chaser", "oldName": "haofairy tale", "description ": "This is a VR tourism e-commerce website "}
{"_ Id": ObjectId ("54d87cdfebde50baebb6c4c9"), "name": "fairy tale", "des": "This is an o2o vr travel platform "}
{"_ Id": ObjectId ("54d87cdfebde50baebb6c4ca"), "city": "Xi'an", "address": "www.dreamerkr.com.cn "}
>Db. web_app.find ({}, {"_ id": 0, "name": 1 })
{"Name": "Mengke "}
{"Name": "fairy tale "}
{}
We can see that only the fields we need to query are displayed here. This is often used in the development of API interfaces. We only want to expose some data, which can enhance data security.
5.2 MongoDB Paging
MongoDB paging mainly uses the Limit () and Skip () methods. Their basic operations are as follows:
(1) Limit () method
To limit records in MongoDB, 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 are learning, you can perform comparative learning, think about how mysql or other database operations are completed in MongoDB, so that you can learn more deeply ).
(2) Syntax:
The basic syntax of the limit () method is as follows: db. COLLECTION_NAME.find (). limit (NUMBER)
(3) Example
>Db. web_app.find ()
{"_ Id": ObjectId ("54d877e4ebde50baebb6c4c8"), "name": "Dream Chaser", "oldName": "haofairy tale", "description ": "This is a VR tourism e-commerce website "}
{"_ Id": ObjectId ("54d87cdfebde50baebb6c4c9"), "name": "fairy tale", "des": "This is an o2o vr travel platform "}
{"_ Id": ObjectId ("54d87cdfebde50baebb6c4ca"), "city": "Xi'an", "address": "www.dreamerkr.com.cn "}
>Db. web_app.find (). limit (2)
{"_ Id": ObjectId ("54d877e4ebde50baebb6c4c8"), "name": "Dream Chaser", "oldName": "haofairy tale", "description ": "This is a VR tourism e-commerce website "}
{"_ Id": ObjectId ("54d87cdfebde50baebb6c4c9"), "name": "fairy tale", "des": "This is an o2o vr travel platform "}
(4) Skip () method
The method skip () also accepts numeric parameters and uses the number of skipped documents. For example, skip (5) means skipping the first five pieces of data and displaying the subsequent data. Note that the default value of the skip () method is 0, which means that you can call the skip without passing the parameter. MongoDB will give a default parameter 0.
(5) syntax
The basic syntax of the skip () method is as follows: db. COLLECTION_NAME.find (). limit (NUMBER). skip (NUMBER)
(6) Example
>Db. web_app.find ()
{"_ Id": ObjectId ("54d877e4ebde50baebb6c4c8"), "name": "Dream Chaser", "oldName": "haofairy tale", "description ": "This is a VR tourism e-commerce website "}
{"_ Id": ObjectId ("54d87cdfebde50baebb6c4c9"), "name": "fairy tale", "des": "This is an o2o vr travel platform "}
{"_ Id": ObjectId ("54d87cdfebde50baebb6c4ca"), "city": "Xi'an", "address": "www.dreamerkr.com.cn "}
>Db. web_app.find (). limit (2). skip (2)
{"_ Id": ObjectId ("54d87cdfebde50baebb6c4ca"), "city": "Xi'an", "address": "www.dreamerkr.com.cn "}
Here is the MongoDB paging operation. The page displays two pieces of data, just like mysql's {begNum}, {endNum}, Which is changed to db. web_app.find (). limit (endNum-begNum ). skip (begNum) reads N records starting from the nth row.
5.3 MongoDB sorting
(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 along with their sorting order. Specify the Order 1 and-1. 1 is used for ascending order, and-1 is used for descending order.
(2) syntax
The basic syntax of the sort () method is as follows: db. COLLECTION_NAME.find (). sort ({KEY: 1 })
(3) Example
If no sorting priority is specified, the sort () method then displays the document in ascending order.
>Db. web_app.find ()
{"_ Id": ObjectId ("54d877e4ebde50baebb6c4c8"), "name": "Dream Chaser", "oldName": "haofairy tale", "description ": "This is a VR tourism e-commerce website "}
{"_ Id": ObjectId ("54d87cdfebde50baebb6c4c9"), "name": "fairy tale", "des": "This is an o2o vr travel platform "}
{"_ Id": ObjectId ("54d87cdfebde50baebb6c4ca"), "city": "Xi'an", "address": "www.dreamerkr.com.cn "}
>Db. web_app.find (). sort ({"name": 1 })
{"_ Id": ObjectId ("54d87cdfebde50baebb6c4ca"), "city": "Xi'an", "address": "www.dreamerkr.com.cn "}
{"_ Id": ObjectId ("54d87cdfebde50baebb6c4c9"), "name": "fairy tale", "des": "This is an o2o vr travel platform "}
{"_ Id": ObjectId ("54d877e4ebde50baebb6c4c8"), "name": "Dream Chaser", "oldName": "haofairy tale", "description ": "This is a VR tourism e-commerce network
5.4 MongoDB Aggregation
Data Records and computation results returned during the aggregation operation. Aggregate operation group values from multiple documents and perform various operations. Grouping data returns a single result. SQL COUNT (*) and groupby are equivalent to MongoDB aggregation.
(1) aggregate () method
The aggregate () method should be used for clustering in MongoDB.
(2) syntax
The basic syntax of the aggregate () method is as follows: db. COLLECTION_NAME.aggregate (AGGREGATE_OPERATION)
(3) Example
>Db. web_info.find ()
{"_ Id": ObjectId ("54d87cdfebde50baebb6c4c9"), "name": "haofairy tale", "address": "This is an o2o vr travel platform "}
{"_ Id": ObjectId ("54db2e0fc50ae3624c1a555c"), "name": "Mengke", "address": "http://www.dreamerkr.com "}
{"_ Id": ObjectId ("54db2e2ac50a6a0d0c6a8c98"), "name": "dream catcher", "address": "http://www.dreamerkr.com "}
{"_ Id": ObjectId ("54db2e36c50a607e6eb3d548"), "name": "dream catcher", "address": "http://www.dreamerkr.com "}
{"_ Id": ObjectId ("54db2e6bc50a5f841ad5c196"), "name": "dream catcher", "address": "http://www.dreamerkr.com "}
{"_ Id": ObjectId ("54db2e6bc50a5f841ad5c197"), "name": "tom "}
{"_ Id": ObjectId ("54db2ebdc50a86203da25e72"), "name": "Mengke", "address": "http://www.dreamerkr.com "}
{"_ Id": ObjectId ("54db2ebdc50a86203da25e73"), "name": "rain beat ranking "}
{"_ Id": ObjectId ("54db2ef5c50a536eb77a3d6d"), "name": "Mengke", "address": "http://www.dreamerkr.com "}
{"_ Id": ObjectId ("54db2ef5c50a536eb77a3d6e"), "name": "rain beat ranking "}
{"_ Id": ObjectId ("54dc0687c50acb624c30dfcb"), "name": "Mengke", "address": "http://www.dreamerkr.com "}
{"_ Id": ObjectId ("54dc0808c50a5945720b1723"), "name": "Dream Chaser", "address": "http://www.dreamerkr.com "}
{"_ Id": ObjectId ("54dc0808c50a5945720b1724"), "name": "rain beat ranking 2 "}
{"_ Id": ObjectId ("54dc09adc50ac37356e0f1a9"), "name": "Dream Chaser "}
{"_ Id": ObjectId ("54dc0a4ac50a4fac80316257"), "name": "Mengke "}
{"_ Id": ObjectId ("54dc0a8cc50ad7af35250807"), "name": "Mengke 2 "}
{"_ Id": ObjectId ("54dc0ac4c50adf3258a884bf"), "name": "Mengke 2 "}
{"_ Id": ObjectId ("54dc0baec50ad2416bac179b"), "name": "Mengke "}
{"_ Id": ObjectId ("54dc0cc3c50afa2987800afe"), "name": "Dream Chaser "}
{"_ Id": ObjectId ("54dc130fc50a1c2e75e2b05c"), "name": "Mengke", "address": "http://www.dreamerkr.com "}
>Db. web_info.aggregate ([{$ group: {_ id: "$ name", num: {$ sum: 1 }}])
{"_ Id": null, "num": 2}
{"_ Id": "rain beat ranking 2", "num": 1}
{"_ Id": "rain beat ranking", "num": 3}
{"_ Id": "tom", "num": 1}
{"_ Id": "Dream Chaser", "num": 13}
{"_ Id": "Dream chaser 2", "num": 2}
{"_ Id": "fairy tale", "num": 3}
The preceding statement is equivalent to SQL query of select name, count (*) from web_info group by name. In the above example, we have grouped the field name document, and the sum of the previous values of each number of times name increases progressively. Other aggregate expressions are listed as follows:
Expression |
Description |
Instance |
$ Sum |
Summarize the values defined by all files in the set. |
Db. web_info.aggregate ([{$ group: {_ id: "$ name", num: {$ sum: "$ address" }}]) |
$ Avg |
The average value calculated from all given values in all document sets. |
Db. web_info.aggregate ([{$ group: {_ id: "$ name", num: {$ avg: "$ address" }}}]) |
$ Min |
Obtain the minimum value of all objects in the set. |
Db. web_info.aggregate ([{$ group: {_ id: "$ name", num: {$ min: "$ address" }}}]) |
$ Max |
Obtains the maximum value of all objects in the set. |
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 |
Values are inserted into an array. |
Db. web_info.aggregate ([{$ group: {_ id: "$ name", address: {$ addToSet: "$ address" }}}]) |
$ First |
The first document retrieved from the source document by group. In general, this makes sense, along with some previous applications "$ sort"-stage. |
Db. web_info.aggregate ([{$ group: {_ id: "$ name", first_address: {$ first: "$ address" }}}]) |
$ Last |
Obtain the final document from the source document by group. Generally, this makes sense, along with the previous application "$ sort"-stage. |
Db. web_info.aggregate ([{$ group: {_ id: "$ name", last_address: {$ last: "$ address" }}}]) |