How to select where sum of fields is greater than a value in MongoDB

kscott

Using MongoDB, How would I write this regular SQL statement?

SELECT * FROM table WHERE (field1+field2+field3) > 1

I've been messing with $group, $project, $add, etc. I feel like I'm dancing all around the solution but can't figure it out.

Salvador Dali

The easiest way to do this is by using $where (I am not telling that it is not possible to do this with aggregation)

db.table.find({$where: function() {
   return this.field1 + this.field2 + this.field3 > 1
   // most probably you have to handle additional cases if some of the fields do not exist.
}}

The pros of it is that it is easy and intuitive, whereas cons:

requires that the database processes the JavaScript expression or function for each document in the collection.

If you need to perform this kind of searches often, I would go ahead and create a new field which will have a sum of 3 fields stored in it and put an index on it. The downside is that you have to increase your app logic.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Select where no value is greater than X

From Dev

sql select where (sum of 2 columns) is greater than X

From Dev

SQL query - select max where a count greater than value

From Dev

How to count rows of pivot table where value is greater than 0

From Dev

Select array elements that are greater than 5% of a sum

From Dev

MySQL select records with sum greater than threshold

From Dev

MySQL to select a daterange where sum is less than a value

From Dev

MySQL count rows where column value is same and select them where count is greater than 2

From Dev

Select items where their amount is greater than 5

From Dev

Linq: Where count greater than value

From Dev

SQL Where clause use if value greater than

From Dev

d3.js v4 Partition where parent's value is greater than sum of its children (node.sum)

From Dev

Select all columns greater than some value

From Dev

Select all columns greater than some value

From Dev

jquery-validate: validate if the sum of two fields is greater than X

From Dev

jquery-validate: validate if the sum of two fields is greater than X

From Java

How to create a cumulative sum column in python if column value is greater than other value

From Dev

How to get Documents less than or greater than some value in mongodb using java

From Dev

Smallest sum of subarray with sum greater than a given value

From Dev

How to find documents where field1 is greater than field2? ( Nodejs/Mongoose/MongoDB)

From Dev

How to find documents where field1 is greater than field2? ( Nodejs/Mongoose/MongoDB)

From Dev

Query where sum of a group of records greater than ZERO

From Dev

Select MySQL results, ignoring fields that have a count greater than X

From Dev

sum if greater than in r

From Dev

SQL select all records only if the sum is greater than 0

From Dev

I want to select branch id,branch location where sum of total item is greter than 11 in mongodb

From Dev

Excel: Select the cell to the left that is greater than/less than some value

From Dev

Number of Subarray whose sum greater than given value

From Dev

SQL select from inner join where count greater than

Related Related

  1. 1

    Select where no value is greater than X

  2. 2

    sql select where (sum of 2 columns) is greater than X

  3. 3

    SQL query - select max where a count greater than value

  4. 4

    How to count rows of pivot table where value is greater than 0

  5. 5

    Select array elements that are greater than 5% of a sum

  6. 6

    MySQL select records with sum greater than threshold

  7. 7

    MySQL to select a daterange where sum is less than a value

  8. 8

    MySQL count rows where column value is same and select them where count is greater than 2

  9. 9

    Select items where their amount is greater than 5

  10. 10

    Linq: Where count greater than value

  11. 11

    SQL Where clause use if value greater than

  12. 12

    d3.js v4 Partition where parent's value is greater than sum of its children (node.sum)

  13. 13

    Select all columns greater than some value

  14. 14

    Select all columns greater than some value

  15. 15

    jquery-validate: validate if the sum of two fields is greater than X

  16. 16

    jquery-validate: validate if the sum of two fields is greater than X

  17. 17

    How to create a cumulative sum column in python if column value is greater than other value

  18. 18

    How to get Documents less than or greater than some value in mongodb using java

  19. 19

    Smallest sum of subarray with sum greater than a given value

  20. 20

    How to find documents where field1 is greater than field2? ( Nodejs/Mongoose/MongoDB)

  21. 21

    How to find documents where field1 is greater than field2? ( Nodejs/Mongoose/MongoDB)

  22. 22

    Query where sum of a group of records greater than ZERO

  23. 23

    Select MySQL results, ignoring fields that have a count greater than X

  24. 24

    sum if greater than in r

  25. 25

    SQL select all records only if the sum is greater than 0

  26. 26

    I want to select branch id,branch location where sum of total item is greter than 11 in mongodb

  27. 27

    Excel: Select the cell to the left that is greater than/less than some value

  28. 28

    Number of Subarray whose sum greater than given value

  29. 29

    SQL select from inner join where count greater than

HotTag

Archive