Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用自己的字段在MongoDB中过滤_Python_Django_Mongodb - Fatal编程技术网

Python 使用自己的字段在MongoDB中过滤

Python 使用自己的字段在MongoDB中过滤,python,django,mongodb,Python,Django,Mongodb,我正试图通过mongodb自己的字段,特别是时间来过滤mongodb文档,即: {time1: timeval1, time2: timeval2} 我希望能够获得一个结果集(如.find()),其中timeval1大于timeval2 我不知道如何使用$gt实现这一点,也找不到类似的东西。有什么办法吗 我在djangi项目中从pymongo访问它。。。但是我想要的只是mongodb查询。您可以使用$where操作符和javascript表达式来编写查询逻辑 >db.newColl.find

我正试图通过mongodb自己的字段,特别是时间来过滤mongodb文档,即:

{time1: timeval1, time2: timeval2}
我希望能够获得一个结果集(如.find()),其中timeval1大于timeval2

我不知道如何使用$gt实现这一点,也找不到类似的东西。有什么办法吗


我在djangi项目中从pymongo访问它。。。但是我想要的只是mongodb查询。

您可以使用
$where
操作符和javascript表达式来编写查询逻辑

>db.newColl.find()
{“_id”:ObjectId(“4fbdcd6e79d66d9e681b185”),“time1”:123,“time2”:22}
{“_id”:ObjectId(“4fbdcddee79d66d9e681b186”),“time1”:11,“time2”:220}
{“_id”:ObjectId(“4fbdcdde6e79d66d9e681b187”),“time1”:331,“time2”:2120}
{“_id”:ObjectId(“4fbdce79d66d9e681b188”),“time1”:1,“time2”:20}
>db.newColl.find({'$where':'this.time1>this.time2'});
{“_id”:ObjectId(“4fbdcd6e79d66d9e681b185”),“time1”:123,“time2”:22}
>db.newColl.find({'$where':'this.time1


使用$where时,最好注意其性能比“正常”查询差得多。mongodb手册:“只有在必须使用$where时才使用它,这会大大降低速度。”相关建议是尽量在$where之前使用普通查询表达式过滤结果集,以减少$where处理的文档数量。
> db.newColl.find()

{ "_id" : ObjectId("4fbdcdd6e79d66d9e681b185"), "time1" : 123, "time2" : 22 }
{ "_id" : ObjectId("4fbdcddee79d66d9e681b186"), "time1" : 11, "time2" : 220 }
{ "_id" : ObjectId("4fbdcde6e79d66d9e681b187"), "time1" : 331, "time2" : 2120 }
{ "_id" : ObjectId("4fbdcdece79d66d9e681b188"), "time1" : 1, "time2" : 20 }

> db.newColl.find({'$where' : 'this.time1 > this.time2' });

{ "_id" : ObjectId("4fbdcdd6e79d66d9e681b185"), "time1" : 123, "time2" : 22 }


> db.newColl.find({'$where' : 'this.time1 < this.time2' });

{ "_id" : ObjectId("4fbdcddee79d66d9e681b186"), "time1" : 11, "time2" : 220 }
{ "_id" : ObjectId("4fbdcde6e79d66d9e681b187"), "time1" : 331, "time2" : 2120 }
{ "_id" : ObjectId("4fbdcdece79d66d9e681b188"), "time1" : 1, "time2" : 20 }