其他分享
首页 > 其他分享> > pymongo 聚合搜索

pymongo 聚合搜索

作者:互联网

首先保存了二手房交易数据,数据形式:

{
    "_id": {
        "$oid": "60b7389ca70ea7f8e951c7da"
    },
    "price_total": "40",
    "price_unit": "4498",
    "trade_date": "2021.05.02",
    "unit_link": "https://cq.lianjia.com/chengjiao/106107543408.html",
    "unit_info": "龙凤二村 3室2厅 88.5平米",
    "price_listing": "挂牌42万",
    "deal_cycle": "成交周期49天",
    "citycode": "cq",
    "city": "重庆",
    "xiaoqu": {
        "picUrl": "https://image1.ljcdn.com/hdic-resblock/717251c1-fd6d-484e-8919-60cfe1ea33a4.jpg",
        "name": "龙凤二村",
        "buildYear": "1978年",
        "buildNum": "68栋",
        "buildType": "板楼/塔板结合",
        "frameNum": 0,
        "frameUrl": "https://cq.lianjia.com/xiaoqu/3620030253941210/huxing/",
        "viewUrl": "https://cq.lianjia.com/xiaoqu/3620030253941210/",
        "unitPrice": 5311,
        "sellNum": 47,
        "rentNum": 5,
        "sellUrl": "https://cq.lianjia.com/ershoufang/c3620030253941210/",
        "rentUrl": "https://cq.lianjia.com/zufang/c3620030253941210/",
        "gonglue": [],
        "district": "北碚",
        "street": "城南新区",
        "on_rent_price": 13.82,
        "rent_to_sale": 0.00369
    },
}

现在要根据‘xiaoqu'中的'district'和'trade_date'来分组,并对每一组计算总数,然后计算小区的平均价格

    pipeline=[
        {"$unwind": "$xiaoqu"},#要查询数组内部元素,需要先拆开数组
        {
            "$group": {
                "_id": {
                    'trade_date':'$trade_date',
                    'city':'$city',
                    'district':'$xiaoqu.district'#查询数组内部元素
                    },
                'count':{'$sum':1},#计数
                'unitPrice':{'$avg':'$xiaoqu.unitPrice'}#计算平均数,数据不能是字符串
                }
            },
        ]
    result=[]
    for col in mycol.aggregate(pipeline):
        col.update({'unitPrice':round(col['unitPrice'])})
        print(col)

结果:

{'_id': {'trade_date': '2021.05.17', 'city': '武汉', 'district': '东西湖'}, 'count': 11, 'unitPrice': 13916}
{'_id': {'trade_date': '2021.05.17', 'city': '武汉', 'district': '青山'}, 'count': 2, 'unitPrice': 15073}
{'_id': {'trade_date': '2021.05.17', 'city': '武汉', 'district': '江夏'}, 'count': 6, 'unitPrice': 13867}
{'_id': {'trade_date': '2021.05.17', 'city': '武汉', 'district': '汉阳'}, 'count': 12, 'unitPrice': 17238}
{'_id': {'trade_date': '2021.05.17', 'city': '武汉', 'district': '江汉'}, 'count': 10, 'unitPrice': 21657}
{'_id': {'trade_date': '2021.05.17', 'city': '武汉', 'district': '蔡甸'}, 'count': 3, 'unitPrice': 8650}
{'_id': {'trade_date': '2021.05.17', 'city': '武汉', 'district': '东湖高新'}, 'count': 27, 'unitPrice': 21799}
{'_id': {'trade_date': '2021.05.17', 'city': '武汉', 'district': '硚口'}, 'count': 4, 'unitPrice': 17556}
{'_id': {'trade_date': '2021.05.17', 'city': '武汉', 'district': '洪山'}, 'count': 23, 'unitPrice': 18348}
{'_id': {'trade_date': '2021.05.17', 'city': '武汉', 'district': '武昌'}, 'count': 17, 'unitPrice': 23702}
{'_id': {'trade_date': '2021.05.17', 'city': '武汉', 'district': '江岸'}, 'count': 19, 'unitPrice': 21293}
{'_id': {'trade_date': '2021.05.17', 'city': '武汉', 'district': '黄陂'}, 'count': 3, 'unitPrice': 10660}
{'_id': {'trade_date': '2021.05.17', 'city': '武汉', 'district': '沌口开发区'}, 'count': 2, 'unitPrice': 17290}

 

标签:city,聚合,district,pymongo,trade,搜索,date,unitPrice,id
来源: https://www.cnblogs.com/moneymaster/p/14845008.html