1

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.

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 :)

Community
  • 1
  • 1
EgzEst
  • 355
  • 3
  • 5
  • 15

1 Answers1

2

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

query = {"event.region": region}
Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195