Reputation: 1654
Assume there is collection like:
db.test.insert([{"f1":100,"f2":150},{"f1":120,"f2":541},{"f1":125,"f2":140}])
How can I create similar query mongodb
Select count(*) from test where (f2-f1)<100
I did some tries, but not work:
db.test.aggregate([{ $match: {} }, { $project: { _id : 0,name : 1, r1: {$subtract:["$f2", "$f1"]} }}])
Upvotes: 2
Views: 2005
Reputation: 69663
First use $project
to create a result-set with the calculated values. Then use $match
to filter out those values which don't match your condition.
db.test.aggregate(
{ $project: { _id : 0,
name : 1,
difference: {$subtract:["$f2", "$f1"]}
}
},
{ $match: { difference: { $lt: 100 }
}
})
Upvotes: 5