简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM