mongo $addToSet获取唯一值实例

来源:互联网 发布:中航微电子重庆 知乎 编辑:程序博客网 时间:2024/06/03 16:10

参考链接   https://docs.mongodb.com/manual/reference/operator/aggregation/addToSet/index.html


语法  

{ $addToSet: <expression> } 

作用:

在group分组的前提下,获取不重复的字段的value  组成的数组


eg:

    $pipeline = [        ['$match' => $where],        ['$group' => [            '_id' => '$cid',            'unique_tel' => [                '$addToSet' => '$tel'            ]        ]],    ];    $stat_data = $this        ->getCollection()        ->aggregate($pipeline); //获取不重复的tel字段    return $stat_data['result'] ? $stat_data['result'] : [];


原创粉丝点击