How to select where sum of fields is greater than a value in MongoDB -
using mongodb, how write regular sql statement?
select * table (field1+field2+field3) > 1 i've been messing $group, $project, $add, etc. feel i'm dancing around solution can't figure out.
the easiest way using $where (i not telling not possible aggregation)
db.table.find({$where: function() { return this.field1 + this.field2 + this.field3 > 1 // have handle additional cases if of fields not exist. }} the pros of is easy , intuitive, whereas cons:
requires database processes javascript expression or function each document in collection.
if need perform kind of searches often, go ahead , create new field have sum of 3 fields stored in , put index on it. downside have increase app logic.
Comments
Post a Comment