在mongoDB集合中,我有不同的字段类型。可以使用update_one方法更新此集合中的文档吗?
from bs4 import BeautifulSoup as bs
import pandas as pd
res = requests.get('https://www.raywhite.com/contact/?type=People&target=people&suburb=Sydney%2C+NSW+2000&radius=50%27%27&firstname=&lastname=&_so=contact', headers = {'User-agent': 'Super Bot 9000'})
soup = bs(res.content, 'lxml')
data = soup.find_all("div",{"class":"card horizontal-split vcard"})
records = []
for item in data:
name = item.find('li', class_='agent-name').text
position = item.find('li',class_='agent-role').text
phone = item.find('li', class_='agent-officenum').text
#link = item.find('li', class_='agent-name')['href']
try:
email = item.find('a', class_='val withicon')['href']
except:
email = 'No Email address'
records.append({'Names':name,'Position':position,'Email':email,'Phone':phone})
for i in records:
nl=pd.unique(name).tolist()
pl=pd.unique(position).tolist()
el=pd.unique(email).tolist()
phl=pd.unique(phone).tolist()
records.append({'Names': nl, 'Position': pl, 'Email': el, 'Phone': phl})
df = pd.DataFrame(records,columns=['Names','Position','Phone','Email'])
df.to_excel(r'C:\Users\laptop\Desktop\RayWhite.xls', sheet_name='MyData2', index = False, header=True)
该代码替换了category字段中的现有数组,但是如果集合doc数组中不存在doc [“ categories”]数组,则需要从中添加值。
答案 0 :(得分:1)
您需要将$push
与$each
一起使用,以将元素添加到现有数组中。 $set
将始终用新值替换现有值。
您可以尝试以下方法:
doc = {"upd_time": datetime.datetime.now(), "categories": [1,2,3]}
mng_collection.update_one({"_id": id}, {
"$set": { "doc.upd_time" : doc.upd_time},
$push : {categories : {$each : doc.categories}}
}, upsert=True)