[英]pymongo using find() to query an array always fails
I've been searching this for about half the evening and I cannot seem to understand or find my error. 我已经搜索了大约半个晚上,但似乎无法理解或发现我的错误。
I am attempting to connect to a MongoDB and search a table for a specific string. 我试图连接到MongoDB并在表中搜索特定的字符串。 I do not care what field the string is in, only that if either field in the row contains the string it should return the contents of the 'name' field.
我不在乎字符串在哪个字段中,只是在行中的任何一个字段包含字符串的情况下,它都应该返回“名称”字段的内容。
I've tried a bunch of different ways of writing this, and I always seem to come up with something along the lines of: 我尝试了多种不同的编写方式,但似乎总是想出一些类似的方法:
search_for_mapname(mpname):
# create the MongoDB client and connect to the database
dbclient = pymongo.MongoClient(dbtoken)
# check for mpname
cursor = dbclient.database.maps.find( { 'name': mpname, "$or":[{"aliases":mpname}] } )
for map in cursor:
return map['name']
return "INVALID"
Each row is structured like this: 每行的结构如下:
{ 'name': "ff_2fort", 'aliases': ["2fort", "2", "alias"] }
find()
seems to recognize when mpname == maps['name']
but when I input an alias that I know exists, I get "INVALID" every time. 当
mpname == maps['name']
时, find()
似乎可以识别,但是当我输入一个已知存在的别名时,每次都会得到“ INVALID”。
我发现了自己的错误,$或上面的代码无法正常工作,实际上应该这样写:
cursor = dbclient.FortressForever.maps.find( { "$or":[ {"name": mpname}, {"aliases":mpname}] } )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.