处理对象
{'_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})