简体   繁体   English

Pymongo中是否有一个复杂的查询生成器?

[英]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. 请直接将我定向到mongodb的任何此类框架,在该框架中我可以编写一个查询,并且可以满足我的要求。

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 . 如果您的意思是“代码行数”,那么您可能想要检查MongoDB聚合框架提供的可能性。

If you mean "memory and speed" optimization, I would not recommend using anything else but MongoDB's basic find command. 如果您要进行“内存和速度”优化,那么我不建议您使用MongoDB的基本find命令之外的其他方法。 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. 根据您在代码中看到的内容,我唯一建议做的更改是确保对“父”键也进行了索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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