Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 879
Gana Avatar asked Apr 25 '12 10:04

Gana


1 Answers

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

like image 184
JohnnyHK Avatar answered Nov 11 '22 16:11

JohnnyHK