[英]Update MongoDB collection using Python
我有一個MongoDB,文檔如下:
我有一個文本文件,其中包含一些單詞及其情感得分。 如果單詞在testcollection中為“ surfaceEnd”,我想更新一些字段。 否則,我要插入新行。
for w in words:
print w
if db.testcollection10.find({ 'surfaceEnd': w }) == True:
posnum = float(get_positive(cols))
negnum = float(get_negative(cols))
db.testcollection.update({ 'surfaceEnd': w}, {"$set": { 'posEnd': posnum,'negEnd': negnum,'findEnd' : 1 }})
i = i + 1
else:
cursor = db.collectionNelly.find({ 'surfaceStart': w })
for document in cursor:
relation = document['rel']
word = document['surfaceEnd'].encode('utf-8')
posnum = float(get_positive(cols))
negnum = float(get_negative(cols))
if 'Synonym' in document['rel']:
db.testcollection1.insert ({ 'surfaceStart': w,'posStart': posnum, 'negStart': negnum, 'surfaceEnd': word,'posEnd': posnum,'negEnd': negnum, 'rel' : document['rel'], 'findEnd' : 0 })
不幸的是,無法創建測試集合。 這段代碼有什么問題?
PyMongo的find操作返回一個游標,而不是布爾值。 您想要確定返回了多少條記錄並采取相應措施。
for w in words:
print w
items = db.testcollection10.find({ 'surfaceEnd': w })
if items.count() > 0:
# The surfaceEnd entry was found update it.
else:
# The surfaceEnd entry was not found, insert a new one.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.