简体   繁体   English

Pymongo或条件过滤查询

[英]Pymongo Or Condition To Filter Queries

so I am having a little bit of trouble going through mongoDB with pymongo and using the or condition. 所以我在使用pymongo遍历mongoDB并使用or条件时遇到了一些麻烦。 Looked at all similar posts, but for some reason, mine doesn't seem to work. 看过所有类似的帖子,但由于某种原因,我的似乎不起作用。

        goodShots = coll.find({"Tag" : "zombiegame", "Meta.levelState":"NORMAL_MODE", "$or":[
            {"Meta.zombiesInLOS":{'$gt':0}}, {"Meta.zombiesInSLOS":{'$gt':0}},
             {"Meta.helpersInLOS":{'$gt':0}}, {"Meta.helpersInSLOS":{'gt':0}}
            ]})

        goodShot01 = coll.find({"Tag" : "zombiegame", "Meta.levelState" : "NORMAL_MODE", "Meta.zombiesInLOS" : {"$gt" : 0}})
        goodShot02 = coll.find({"Tag" : "zombiegame", "Meta.levelState" : "NORMAL_MODE", "Meta.zombiesInSLOS" : {"$gt" : 0}})
        goodShot03 = coll.find({"Tag" : "zombiegame", "Meta.levelState" : "NORMAL_MODE", "Meta.helpersInLOS" : {"$gt" : 0}})
        goodShot04 = coll.find({"Tag" : "zombiegame", "Meta.levelState" : "NORMAL_MODE", "Meta.helpersInSLOS" : {"$gt" : 0}})

The first query 'goodShots' should technically return queries that have all the other conditions in 'goodShot01-04.' 从技术上讲,第一个查询“ goodShots”应返回具有“ goodShot01-04”中所有其他条件的查询。 However, it returns 0. 'goodShot01-04' returns a count > 0 though. 但是,它返回0。虽然'goodShot01-04'返回的计数> 0。

If I insert 4 documents like 如果我插入4个文档,例如

{ 
    "Tag" : "zombiegame",
    "Meta" : {
        "levelState" : "NORMAL_MODE",
        "zombiesInLOS" : 1
    }
}

varying the last field in Meta through the 4 possibilities, then I find all four documents with the goodShots query and each appropriate document for goodShot01 through goodShot04 . 通过4种可能性改变Meta的最后一个字段,然后我找到了所有带有goodShots查询的四个文档,并通过goodShot04找到了适用于goodShot01每个文档。 I had to add in a missing $ in the last clause of the $or to find 4 instead of 3 documents. 我必须在$的最后一个子句中添加一个缺失的$or找到4个而不是3个文档。 Perhaps check again that you are putting in the right query? 也许再次检查您是否输入了正确的查询?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM