我有一个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 })
不幸的是,无法创建testcollection。这段代码有什么问题?
答案 0 :(得分:1)
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.