mongodb How to make age field in sum not count

OKie

If you see, age field and using sum operator in mongodb is the same as normal SQL count operator. Can you please explain how to use correct sum in mongodb

> db.test.find()
{
        "_id" : ObjectId("544654d01cdcaf6a66c188e8"),
        "age" : 23,
        "name" : "Alex"
}
{
        "_id" : ObjectId("544654f01cdcaf6a66c188ec"),
        "age" : 33,
        "name" : "Alex"
}
{
        "_id" : ObjectId("544654fb406947798c239544"),
        "age" : 14,
        "name" : "Alex"
}
{
        "_id" : ObjectId("5446550a406947798c239547"),
        "age" : 25,
        "name" : "Oleg"
}
{
        "_id" : ObjectId("54465523406947798c23954a"),
        "age" : 15,
        "name" : "Oleg"
}
{
        "_id" : ObjectId("54465538406947798c23954e"),
        "age" : 10,
        "name" : "Mark"
}


> db.test.aggregate([{$group:{_id:"$name","age":{$sum:1}}}])
{ "_id" : "Mark", "age" : **1** }
{ "_id" : "Oleg", "age" : **2** }
{ "_id" : "Alex", "age" : **3** }
Wizard

You can write as

db.test.aggregate([{$group:{_id:"$name","age":{$sum:"$age"}}}])

to sum as you expected.

The operand of $sum is an expression, sum is made according to the final result of this expression, added to filed age for this group.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to count and sum a field between 2 dates?

From Java

How to aggregate sum in MongoDB to get a total count?

From Dev

How to make a sum which involves a count

From Dev

How to "group by" field "count" in mongodb querybuilder?

From Dev

How To Make Make Auto Sum Calculation Input Field

From Dev

How To Make Make Auto Sum Calculation Input Field

From Dev

How to sum distinct values of a field in a MongoDB collection (utilizing mongoose)

From Dev

How to sum distinct values of a field in a MongoDB collection (utilizing mongoose)

From Dev

how can I find out the sum of a field in mongodb

From Dev

How to make query with grouping by foreign keys with ordering by sum of field

From Dev

Calculated field based on sum and count of other field

From Dev

how to count number of distinct values of a field from two collections in mongodb

From Dev

MongoDB, PyMongo how to filter results by count of uniques field?

From Dev

How to Group Documents by a Field and Calculate Count in mongodb using java

From Dev

How can query in MongoDB that count number of 2 equal field?

From Dev

How to draw chart in excel with age and its count

From Dev

How to make a age calculation output age when datestring in different order

From Dev

sum field value after using if and count in sql

From Dev

How to Count Number of in in a field

From Dev

Mongodb aggregation - sum a grouped, addToSet field

From Dev

Mongo Aggregate: Sum Count Field of Duplicate Array Value Field

From Dev

How to sort by count on Mongodb

From Dev

Mongodb query and count occurrence of the sub document field

From Dev

count the subdocument field and total amount in mongodb

From Dev

mongodb php: count the number of field in a set of documents

From Dev

mongodb count value of one field based on other

From Dev

MongoDB Count based on output on one field

From Dev

MongoDB: GROUP BY conditionally aggregating COUNT on boolean field

From Dev

multi sum/count on mongodb (sum gender and total all results)

Related Related

  1. 1

    How to count and sum a field between 2 dates?

  2. 2

    How to aggregate sum in MongoDB to get a total count?

  3. 3

    How to make a sum which involves a count

  4. 4

    How to "group by" field "count" in mongodb querybuilder?

  5. 5

    How To Make Make Auto Sum Calculation Input Field

  6. 6

    How To Make Make Auto Sum Calculation Input Field

  7. 7

    How to sum distinct values of a field in a MongoDB collection (utilizing mongoose)

  8. 8

    How to sum distinct values of a field in a MongoDB collection (utilizing mongoose)

  9. 9

    how can I find out the sum of a field in mongodb

  10. 10

    How to make query with grouping by foreign keys with ordering by sum of field

  11. 11

    Calculated field based on sum and count of other field

  12. 12

    how to count number of distinct values of a field from two collections in mongodb

  13. 13

    MongoDB, PyMongo how to filter results by count of uniques field?

  14. 14

    How to Group Documents by a Field and Calculate Count in mongodb using java

  15. 15

    How can query in MongoDB that count number of 2 equal field?

  16. 16

    How to draw chart in excel with age and its count

  17. 17

    How to make a age calculation output age when datestring in different order

  18. 18

    sum field value after using if and count in sql

  19. 19

    How to Count Number of in in a field

  20. 20

    Mongodb aggregation - sum a grouped, addToSet field

  21. 21

    Mongo Aggregate: Sum Count Field of Duplicate Array Value Field

  22. 22

    How to sort by count on Mongodb

  23. 23

    Mongodb query and count occurrence of the sub document field

  24. 24

    count the subdocument field and total amount in mongodb

  25. 25

    mongodb php: count the number of field in a set of documents

  26. 26

    mongodb count value of one field based on other

  27. 27

    MongoDB Count based on output on one field

  28. 28

    MongoDB: GROUP BY conditionally aggregating COUNT on boolean field

  29. 29

    multi sum/count on mongodb (sum gender and total all results)

HotTag

Archive