Filtering in MongoDB with custom fields

I am trying to filter mongodb documents by their fields, in particular time, i.e.

{time1: timeval1, time2: timeval2}

I want to be able to get a set of results (e.g. with .find ()), where timeval1 is greater than timeval2.

I do not know how to do this with $ gt and could not find anything like it. Any ideas on how to do this?

I am accessing it from pymongo inside a djangi project ... but all I want is a mongodb request.

+3
source share
1 answer

You can use the $wherejavascript operator and expression to write the request logic.

http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-JavascriptExpressionsand%7B%7B%24where%7D%7D

> 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 }

+3
source

All Articles