简体   繁体   English

使用pymongo过滤器数据查找与位置

[英]Using pymongo filter data find v.s where

I want to filter data using pymongo. 我想使用pymongo过滤数据。 But I don't know how is this two methods different. 但是我不知道这两种方法有何不同。 The Result show first method's performance is better than second. 结果表明,第一种方法的性能优于第二种方法。 But I already filter a query set list in my storage. 但是我已经在我的存储中过滤了一个查询集列表。 Is this mongodb feature? 这是mongodb功能吗? or pymongo feature? 或pymongo功能?

This Following is my example cases: 以下是我的示例案例:

  1. Use find method:(faster) 使用查找方法:(更快)

     result = pymongo.db.mycollection.find({ condition1, condition2 }) 
  2. Use find method & where expressions:(slower) 使用查找方法和where表达式:(较慢)

     query_data = pymongo.db.mycollection.find({ condition1 }) result = query_data.where(Code("function() { return condition2}")) 

In official documentation, mentions that if can use standard operators avoiding use $where expression. 在官方文档中,提到如果可以使用标准运算符,请避免使用$ where表达式。 Doc. 文档。

You've basically given the answer already yourself. 您基本上已经自己给出了答案。 The first version should be your preferred option as the second one using $where will result in slower execution times. 第一个版本应该是您的首选,因为第二个版本使用$where会导致执行时间变慢。 One of the reasons for that is eg that $where will not use any indexes ( link ). 原因之一是例如$where将不使用任何索引( link )。

You can see the queries that the PHP driver really sends to MongoDB in the server logs by enabling logging on the server like this: 通过在服务器上启用日志记录,您可以在服务器日志中看到PHP驱动程序真正发送到MongoDB的查询:

db.setLogLevel(1) db.setLogLevel(1)

Also, once you know the precise queries you can use .explain() to compare the different execution plans. 同样,一旦知道了精确的查询,就可以使用.explain()比较不同的执行计划。

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

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