我正在尝试按其自己的字段(特别是时间)过滤 mongodb 文档,即:
{time1: timeval1, time2: timeval2}
我希望能够获得 timeval1 大于 timeval2 的结果集(如 .find())。
我不知道如何用 $gt 做到这一点,也找不到类似的东西。关于如何做到这一点的任何想法?
我从 djangi 项目中的 pymongo 访问它......但我想要的只是 mongodb 查询。
您可以使用$where
运算符和 javascript 表达式来编写查询逻辑。
> 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 }