Number of occurrences of individual elements in an array of MongoDB statistics documents (array)

Source: Internet
Author: User
Tags mongodb server

One, the problem description

"Use unwind unpack each element in the array in Document, then use Group grouping statistics, and then sort the grouped results using sort"

Import data from the Images.json file to the MongoDB server

Mongoimport--drop-d test-c Images Images.json

which An example of document is as follows:

 >  db.images.find ()  {"_id": 3, "height": 480, "width": 640,  "tags" : ["Kittens", "Travel"]} {"_id": 1, "height": 480, "width": 640, "tags": ["C ATS "," sunrises "," Kittens "," travel "," Vacation "," work "} {" _id ": 0," height ": 480," width ": 640," tags ": [" Dogs " , "work"} {"_id": 6, "height": 480, "width": 640, "tags": ["Work"] {"_id": 4, "height": 480, "width": 640, " Tags ": [" Dogs "," sunrises "," Kittens "," Travel "]} {" _id ": 5," height ": 480," width ": 640," tags ": [" Dogs "," cats " , "sunrises", "Kittens", "work"} {"_id": 7, "height": 480, "width": 640, "tags": ["Dogs", "sunrises"]} {"_id": 8, "height": 480, "width": 640, "tags": ["Dogs", "Cats", "sunrises", "Kittens", "Travel"]}  

Now count: The number of occurrences of each element within the tags array in all document. That is: How many times has "kittens" appeared? How many times has "travel" appeared? How many times has "dogs" appeared? ......

Second, the implementation steps

Implementation using MongoDB's aggregate operation

① using unwind to decompose tags array, the results are as follows:

> Db.images.aggregate (... [... {$unwind: "$tags"} ...]) {"_id": 3, "height": 480, "width": 640, "tags": "Kittens"} {"_id": 3, "height": 480, "width": 640, "tags": "Travel"} {"_id": 1, "height": 480, "width": 640, "tags": "Cats"} {"_id": 1, "height": 480, "width": 640, "tags": "sunrises"} {"_id": 1, "height": 480, "width": 640, "tags": "Kittens"} {"_id": 1, "height": 480, "width": 640, "tags": "Travel"} {"_id": 1, "height": 480, "width": 640, "tags": "Vacation"} {"_id": 1, "height": 480, "width": 640, "tags": "Work"} {"_id": 0, "height": 480, "width": 640, "tags": "Dogs"} {"_id": 0, "height": 480, "width": 640, "tags": "Work"} {"_id": 6, "height": 480, "width": 640, "tags": "Work"} {"_id": 4, "height": 480, "width": 640, "tags": "Dogs"} {"_id": 4, "height": 480, "width": 640, "tags": "sunrises"} .....

② group operation for each tag after decomposition

For group operations, _id specifies the field to be grouped (which field is the group by operation), and the results generated after the group operation are identified by the Num_of_tag field

> Db.images.aggregate (... [... {$unwind: "$tags"},... {$group: {_id: "$tags", num_of_tag:{$sum: 1}}} ...] ... ) {"_id": "Dogs", "Num_of_tag": 49921} {"_id": "Work", "Num_of_tag": 50070} {"_id": "Vacation", "Num_of_tag": 50036} {"_id": "Travel", "Num_of_tag": 49977} {"_id": "Kittens", "Num_of_tag": 49932} {"_id": "sunrises", "Num_of_tag": 49887} {"_id": "Cats", "Num_of_tag": 49772}

③ use Project to get rid of the _id fields that are not interesting (in fact, this is to replace the _id field name with the tags field name) (This step can be ignored)

Project Operations, _id:0 The _id field is removed, tags: "$_id", the _id field value is identified with the tags field, num_of_tag:1 reserved Num_of_tag field

> Db.images.aggregate ([{$unwind: $tags},{$group: {_id: ' $tags ', num_of_tag:{$sum: 1}}},{$project: {_id:0, Tags: "$_id", Num_of_tag:1}}]){"Num_of_tag": 49921, "tags": "Dogs"} {"Num_of_tag": 50070, "tags": "Work"} {"num _of_tag ": 50036," tags ":" Vacation "} {" Num_of_tag ": 49977," tags ":" Travel "} {" Num_of_tag ": 49932," tags ":" Kitten S "} {" Num_of_tag ": 49887," tags ":" sunrises "} {" Num_of_tag ": 49772," tags ":" Cats "}

④ to sort num_of_tag fields using sort

> Db.images.aggregate ([{$unwind: $tags},{$group: {_id: ' $tags ', num_of_tag:{$sum: 1}}},{$project: {_id:0, Tags: "$_id", num_of_tag:1}},{$sort: {num_of_tag:-1}}]){"Num_of_tag": 50070, "tags": "Work"} {"Num_of_tag": 50036 , "tags": "Vacation"} {"Num_of_tag": 49977, "tags": "Travel"} {"Num_of_tag": 49932, "tags": "Kittens"} {"Num_of_ta G ": 49921," tags ":" Dogs "} {" Num_of_tag ": 49887," tags ":" sunrises "} {" Num_of_tag ": 49772," tags ":" Cats "}

Three, summary

This article is a job in the MongoDB University M101 course for Java developers. Combined with Google search and MongoDB's official documentation, it's easy to implement various combinations of MONGODB queries.

Original: http://www.cnblogs.com/hapjin/p/7944404.html

Number of occurrences of individual elements in an array of MongoDB statistics documents (array)

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.