As Bugai13 said, you cannot perform a comparison across 2 fields in a query.
The problem with $ where is performance is because it is a javascript function that will be executed for each document, so it will have to scan everything.
So, you can save another field (which you could then index) next to these existing fields
eg.
{ "limitcount": 10000, "currentcount": 100, "remainingcount" : 9900 }
so that you can request a new field instead:
db.countcollection.find({"remainingcount" : {$gt : 0}}) db.countcollection.find({"remainingcount" : {$lt : 0}})
source share