關於mongodb的 數組分組 array group

來源:互聯網
上載者:User

標籤:

mongodb支援array類型

在group的功能方面,mongodb確實不錯。對於複雜的資料處理用mapreduce,但是這個玩意很慢,比較適合指令碼處理資料,不適合即時資料查詢

在即時資料處理方面得用管道,關於管道文法方面,參看:

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

如果$class_artist是數組,需要對數組裡面的資料進行group,那麼可以使用$unwind

,它的功能就是把數組拆分出來,形成多條資料。

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_artist: "$_id", tags: 1 } },  {$sort: { tags: -1 } }])

在yii2架構中:

可以在Query.php中加入一個函數:

對 $filter_size數組拆分,然後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' => [                    '?sum'=> 1,                ],            ]        ];$pipelines[] = ['?sort'=> ['_id'=> 1] ];        $result = $collection->aggregate($pipelines);return $result;       }


關於mongodb的 數組分組 array group

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.