Pymongo query with Dictionary inside Dictionary?

I have a document in MongoDB as follows:

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

I want to request mongoDb using the Pymongo findAPI , but it does not work:

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

Nothing is printed with the above code.

+5
source share
1 answer

Two things:

  • If you want to treat 5 in your document as an integer, do not add it in double quotes.
  • Use dot notation to request attached documents:

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

+9
source

All Articles