About the array of MongoDB arrays Group array

Source: Internet
Author: User

MongoDB supports array type

MongoDB is really good at the function of the group. For complex data processing with mapreduce, but this thing is very slow, more suitable for script processing data, not suitable for real-time data query

In the field of real-time data processing to use pipelines, about the pipeline syntax, see:

http://blog.csdn.net/terry_water/article/details/43529367

If $class_artist is an array, you need to group the data inside the arrays, then you can use $unwind

, its function is to split the array, forming a number of data.

Db.articles.aggregate ([  {$match: {class_date: {$gte: Date}}},  {$project: {_id:0, class_artist:1}},  { $unwind: "$class _artist"},  {$group: {_id: "$class _artist", Tags: {$sum: 1}}},  {$project: {_id:0,class_artis T: "$_id", Tags:1}},  {$sort: {tags:-1}}])

In the YII2 framework:

You can add a function to the query.php:

Splits the $filter _size array, and then group.

Public Function filtersize ($db = null) {$collection = $this->getcollection ($db);        $pipelines = [];        if ($this->where!== null) {            $pipelines [] = ['? match ' = $collection->buildcondition ($this->where)];        } $pipelines [] = ['? unwind ' = "\ $filter _size"];        $pipelines [] = [            ' $group ' + = [                ' _id ' + ' \ $filter _size ', ' num_total ' and ' = '                    sum ' = 1,                ] ,            ]        ]; $pipelines [] = ['? sort '] = [' _id ' = 1]];        $result = $collection->aggregate ($pipelines); return $result;       }


About the array of MongoDB arrays Group 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.