Gana
Gana

Reputation: 1027

Pymongo Query with Dictionary inside Dictionary?

I have Document in MongoDB like this:

{"ONE": {"TWO": {"THREE":"5"}}}

I want to query mongoDb using the Pymongo find API, but it's not working:

for value in dbaccess.find({"ONE":{"TWO":{"THREE":{"$gt":"0"}}}}):
     print value

Nothing is getting printed with the above code.

Upvotes: 9

Views: 6732

Answers (1)

JohnnyHK
JohnnyHK

Reputation: 312149

Two things:

  1. If you want to treat the 5 in your document as an integer, don't enclose it in double quotes.
  2. Use dot notation for querying nested documents:

    dbaccess.find("ONE.TWO.THREE": {"$gt": 0})

Upvotes: 14

Related Questions