【问题标题】:pymongo using find() to query an array always failspymongo 使用 find() 查询数组总是失败
【发布时间】:2018-12-16 05:01:15
【问题描述】:

我已经搜索了大约半个晚上,但我似乎无法理解或找到我的错误。

我正在尝试连接到 MongoDB 并在表中搜索特定字符串。我不在乎字符串在哪个字段中,只要行中的任一字段包含字符串,它就应该返回“名称”字段的内容。

我尝试了很多不同的方式来写这篇文章,我似乎总是想出一些类似的东西:

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"

每一行的结构如下:

{ 'name': "ff_2fort", 'aliases': ["2fort", "2", "alias"] }

find() 似乎可以识别 mpname == maps['name'] 的时间,但是当我输入我知道存在的别名时,每次都会得到“INVALID”。

【问题讨论】:

    标签: python arrays mongodb-query find pymongo


    【解决方案1】:

    我发现我的错误,$or不像我上面写的那样工作,它实际上应该这样写:

    cursor = dbclient.FortressForever.maps.find( { "$or":[ {"name": mpname}, {"aliases":mpname}] } )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-30
      • 2021-07-20
      • 1970-01-01
      • 2019-11-09
      • 1970-01-01
      • 1970-01-01
      • 2012-04-27
      相关资源
      最近更新 更多