MongoDB中的光标不';不能正常工作-PYTHON
我有一个MongoDB收藏,如下图所示: 我想按rel筛选MongoDB数据。如果relation(“rel”)是“同义词”或“RelatedTo”,我想在变量中添加文档[surfaceStart]。之后,我想在一个文本文件中搜索,如果这个变量包含在这个txt的一个单词中。 我的代码如下:MongoDB中的光标不';不能正常工作-PYTHON,python,mongodb,Python,Mongodb,我有一个MongoDB收藏,如下图所示: 我想按rel筛选MongoDB数据。如果relation(“rel”)是“同义词”或“RelatedTo”,我想在变量中添加文档[surfaceStart]。之后,我想在一个文本文件中搜索,如果这个变量包含在这个txt的一个单词中。 我的代码如下: f=open(document.txt) cursor = db.collection.find({"$or": [{"rel": "Synonym"}, {"rel": "RelatedTo"}]}) f
f=open(document.txt)
cursor = db.collection.find({"$or": [{"rel": "Synonym"}, {"rel": "RelatedTo"}]})
for document in cursor:
end = document['surfaceEnd'].encode('utf-8')
start = document['surfaceStart'].encode('utf-8')
for line in f:
for w in words:
if start == w:
print start
items = db.collectionNew.find({ 'surfaceEnd': start })
if items.count() > 0:
break
else:
db.collectionNew.insert ({ 'surfaceStart': start,'posStart': posnum, 'negStart': negnum, 'surfaceEnd': end,'posEnd': posnum,'negEnd': negnum, 'rel' : document['rel'], 'findEnd' : 0 })
不幸的是,最后collectionNew仍然是空的。我试图找出问题所在,因此我添加了行
打印行
,我意识到问题从一开始就出现了。我想我的光标有问题。你能帮帮我吗?提前感谢 所有rel
字段值是否都以/r/
为前缀?如果是,您应该将查询更改为:
cursor = db.collection.find({"$or": [{"rel": "/r/Synonym"}, {"rel": "/r/RelatedTo"}]})
如果关系(“rel”)是“同义词”或“RelatedTo”
您的第一个查询是否实际返回任何结果
不,不是 您的图像不显示值为
同义词
或related to
对命令的响应:
以下行显然是错误的,会产生错误:
f=open(document.txt)
endWord = document('surfaceEnd').encode('utf-8')
TypeError: 'dict' object is not callable
不幸的是,有一个错误:
f=open(document.txt)
endWord = document('surfaceEnd').encode('utf-8')
TypeError: 'dict' object is not callable
括号,大括号,括号。它们不能互换
document('surfaceEnd')
应该是:
document['surfaceEnd']
document
是一个python字典,而不是一个函数。您的第一个查询是否实际返回任何结果?如果它们都是/r/
的前缀,如您的屏幕截图所示,则不会返回任何数据。不,不会,但我不知道如何更改它们,以便获得结果:/Downvote的可能重复:OP已经问了相同的问题3次。这次OP改变了问答的本质,引出了两个不同的答案。.encode()。我会试试的,我会尽快通知你!!!!非常感谢现在我在编码方面还有一个问题。如果我有编码('utf8'),它会继续工作,但不正常,如果我删除它,会抛出以下错误。startWord=document['surfaceStart'].encode('utf-8')AttributeError:'NoneType'对象没有图像的属性'encode'。我有很多数据,包括“同义词”和“relatedTo”。现在我有另一个编码问题。如果我有编码('utf8'),它会继续工作,但不正常,如果我删除它,会抛出以下错误。startWord=document['surfaceStart'].encode('utf-8')AttributeError:'NoneType'对象没有属性'encode'@Nelly Brili,如果我有encode('utf8'),它会继续工作,但不正常,如果我删除它会抛出以下错误startWord=document['surfaceStart'].encode('utf-8')
AttributeError:'NoneType'对象没有属性'encode'--您能解释一下在那行代码中删除encode()的地方吗?我删除了.encode('utf-8')
,我只有startWord=document['surfacestartart']
@NellyBrili,那一行代码的错误消息是什么?如果我删除.encode('utf-8')
,就不会有错误消息。但是如果我使用print startWord
或print document['surfaceStart']
它不会像don那样打印任何东西;Don’不要读这个短语