Raj Damani Raj Damani - 9 days ago 4
Python Question

Is there a complex query builder in Pymongo?

The structure of document is as follows:

{ "_id" : 1, "name" : "Demographics", "parent" : 0 }


I want do like as follow:

pyList=list(db.collection.find({"parent":0},{"_id":1}))
colList=list(db.collection.find({"parent":$in:[pyList]},{"name":1}))


As i have achieved my requirement but I am sure even that this is not optimized.
Kindly direct me to any such framework of mongodb where I can write one query and my requirement will be achieved.

Please be gentle as I am newbie.

Answer

It depends on your definition of optimized. If you mean "number of lines of code" then you might want to check the possibilities offered by MongoDB's aggregation framework.

If you mean "memory and speed" optimization, I would not recommend using anything else but MongoDB's basic find command. The reason for this is that a well-indexed collection will always provide faster query results when compared with an aggregation command. From what I see in your code, the only change I would recommend is to make sure the "parent" key is also indexed.

Comments