python 用正则表达式查找 多线程增删改查 MongoDB数据库 全网最高效增删改查mongodb

处理对象

在这里插入图片描述

{'_id': ObjectId('62f60bc45cc1f7853cb9a959'),
  'province': '安徽',
  'city': '铜陵',
  'city_url': '//tl.fang.ke.com',
  'city_end_url': 'https://tl.fang.ke.com/xiaoqu/'},
 {'_id': ObjectId('62f60bc45cc1f7853cb9a95b'),
  'province': '安徽',
  'city': '黄山',
  'city_url': '//huangshan.fang.ke.com',
  'city_end_url': 'https://huangshan.fang.ke.com/xiaoqu/'},
 {'_id': ObjectId('62f60bc45cc1f7853cb9a95d'),

$regex正则 删除city_url里面有fang这个文字的数据

 mycol.remove({"city_url":{"$regex":"fang"}})

在这里插入图片描述

在这里插入图片描述

高德地址转经纬度

def Gaode_address_transform_longitude_and_latitude(object1):
    url = 'https://restapi.amap.com/v3/geocode/geo'
    # url = 'https://restapi.amap.com/v3/geocode/geo'
    # 将两个参数放入字典
    params = {'key': '自己申请的高德key',
              'address': object1["city"] + object1["region"] + object1["projectName"],
              'city': object1["city"]}
    res = requests.get(url, params)
    # 输出结果为json,将其转为字典格式
    jd = json.loads(res.text)
    # 经纬度
    coords = jd['geocodes'][0]['location'].split(",")
    center = {"lng": coords[0], "lat": coords[1]}
    return center

拿id查找,然后实现更改字段名称,循环更新mongodb的新字段

for i in mycol.find():#循环自己的id
    print(mycol.find({'_id':i['_id']}))#必须要拿id具体查找到才能插入对应数据i['_id']
    mycol.update({'_id':i['_id']},{"$set":{'center':Gaode_address_transform_longitude_and_latitude(i)}},False,True)

多线程更新字段mongodb–插入数据

list_=[i["_id"] for i in list(mycol.find())]
def pross1(id_):
    companycol.update({'_id':id_}, {"$set":{'center':list(mycol.find({'_id':id_}))[0]['center']}}, False, True)
pool = ThreadPool(10)
pool.map(pross1,list_)
pool.close()
pool.join()

多线程更新mongodb–更换字段名

from multiprocessing.dummy import Pool as ThreadPool
list_=[i["_id"] for i in list(mycol.find())]
def pross(id_):
    mycol.update({'_id':id_}, {'$rename': {'updateDate': 'priceMonth'}}, False, True)
pool = ThreadPool(10)
pool.map(pross,list_)
pool.close()
pool.join()

深度查询

目标 查询企业资质为监理资质的企业
在这里插入图片描述

{"certList":{"$elemMatch":{"certType":"监理资质"}}}

在这里插入图片描述
[{‘KaTeX parse error: Expected '}', got 'EOF' at end of input: …_id':{'bqyId':'bqyId’}, ‘uniqueIds’:{‘ a d d T o S e t ′ : ′ addToSet':' addToSet:_id’}, ‘count’:{‘KaTeX parse error: Expected 'EOF', got '}' at position 7: sum':1}̲ }}, {'match’:{‘count’:{‘$gt’:1}}}], {allowDiskUse:true}

失败1
[{‘KaTeX parse error: Expected '}', got 'EOF' at end of input: …{'辽宁晟威建设有限公司':'辽宁晟威建设有限公司’}, ‘uniqueIds’:{‘KaTeX parse error: Expected 'EOF', got '}' at position 27: …enterpriseName'}̲, 'count':{'sum’:1} }}, {‘KaTeX parse error: Expected '}', got 'EOF' at end of input: …ch':{'count':{'gt’:1}}}], {allowDiskUse:true}

.aggregate([{KaTeX parse error: Expected '}', got 'EOF' at end of input: …roup: { _id : 'trend_id’, count: {KaTeX parse error: Expected 'EOF', got '}' at position 7: sum: 1}̲}}, {match: {count: {$gt: 1}}}], {allowDiskUse: true})

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值