Let's start with getting some values to play with;
> for(var i = 1; i <= 10; i++) { db.test.save({a : i, b: 10-i}); }
WriteResult({ "nInserted" : 1 })
> db.test.find()
{ "_id" : ObjectId("5a5c997b28e6f31805330889"), "a" : 1, "b" : 9 }
{ "_id" : ObjectId("5a5c997b28e6f3180533088a"), "a" : 2, "b" : 8 }
{ "_id" : ObjectId("5a5c997b28e6f3180533088b"), "a" : 3, "b" : 7 }
{ "_id" : ObjectId("5a5c997b28e6f3180533088c"), "a" : 4, "b" : 6 }
{ "_id" : ObjectId("5a5c997b28e6f3180533088d"), "a" : 5, "b" : 5 }
{ "_id" : ObjectId("5a5c997b28e6f3180533088e"), "a" : 6, "b" : 4 }
{ "_id" : ObjectId("5a5c997b28e6f3180533088f"), "a" : 7, "b" : 3 }
{ "_id" : ObjectId("5a5c997b28e6f31805330890"), "a" : 8, "b" : 2 }
{ "_id" : ObjectId("5a5c997b28e6f31805330891"), "a" : 9, "b" : 1 }
{ "_id" : ObjectId("5a5c997b28e6f31805330892"), "a" : 10, "b" : 0 }
We can now use the aggregation framework with a $cond:
> db.test.aggregate([
... { $project: { aGreaterThanb: { $cond: { if: { $gt: [ "$a", "$b" ] }, then: true, else: false } } } }
...
... ]);
{ "_id" : ObjectId("5a5c997b28e6f31805330889"), "aGreaterThanb" : false }
{ "_id" : ObjectId("5a5c997b28e6f3180533088a"), "aGreaterThanb" : false }
{ "_id" : ObjectId("5a5c997b28e6f3180533088b"), "aGreaterThanb" : false }
{ "_id" : ObjectId("5a5c997b28e6f3180533088c"), "aGreaterThanb" : false }
{ "_id" : ObjectId("5a5c997b28e6f3180533088d"), "aGreaterThanb" : false }
{ "_id" : ObjectId("5a5c997b28e6f3180533088e"), "aGreaterThanb" : true }
{ "_id" : ObjectId("5a5c997b28e6f3180533088f"), "aGreaterThanb" : true }
{ "_id" : ObjectId("5a5c997b28e6f31805330890"), "aGreaterThanb" : true }
{ "_id" : ObjectId("5a5c997b28e6f31805330891"), "aGreaterThanb" : true }
{ "_id" : ObjectId("5a5c997b28e6f31805330892"), "aGreaterThanb" : true }
$cond operator