2019 十二月 7 , 星期六
Home / 开发语言 / python / pymongo 聚合查询group

pymongo 聚合查询group

获取重复的手机号
db.weikephone.insert({“wid”:100000185, “phone”: 13818070900})
db.weikephone.insert({“wid”:100000186, “phone”: 13818070900})
db.weikephone.insert({“wid”:100000187, “phone”: 13818070901})
db.weikephone.insert({“wid”:100000188, “phone”: 13818070902})

import pymongo
mondb = pymongo.MongoClient('localhost',27017)
mondb = mondb["test"]
pipline = [
        {"$group" : {"_id" : "$phone", "count" : {"$sum" : 1}}},
        {"$match" : {"count" : {"$gt" : 1}}}
    ]
print list(mondb.weikephone.aggregate(pipline))

等同于 select phone,count(*) as count from t_wm_weikephone group by phone having count>1

Check Also

python 解决 InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information

因为SSL的问题,urllib ...

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>