mongo查询不返回结果[重复]

时间:2022-09-06 08:12:17

This question already has an answer here:

这个问题已经有了答案:

I have a document like this is database

我有一个这样的文件,这是数据库

{
    "timestamp" : "2016-06-27T14:29:22.091Z",
    "story" : "This is the story of how I was harassed.",
    "latitude": 42.23424364766,
    "longitude": 21.13243546545,
    "event": {
        "region": "Albania",
        "language": "English",
        "version": 1
    }
}

under event key is region. It can be different.

在事件键下是区域。它可以是不同的。

Now using python, I want to make a query on mongodb and I want to return only the documents that have "event" : {'region': region} region is a parameter that can change.

现在使用python,我想在mongodb上做一个查询,我只想返回带有“event”的文档:{'region': region}区域是一个可以更改的参数。

def get_all_event_based_on_region(self,region):
    query = { "event" : {'region': region}}
    ...
    docs = self._get(query)
    return list(docs)

But this query return nothing cause there are other fields like language and version, how can I modify this query so it will works?

但是这个查询没有返回任何内容,因为还有其他字段,比如语言和版本,我如何修改这个查询,使它可以工作?

Thanks in advance :)

提前谢谢:)

1 个解决方案

#1


2  

Referring to How to query nested objects? - you need to transform your query into:

关于如何查询嵌套对象?-你需要把你的查询转换成:

query = {"event.region": region}

#1


2  

Referring to How to query nested objects? - you need to transform your query into:

关于如何查询嵌套对象?-你需要把你的查询转换成:

query = {"event.region": region}