In Spring MongoDB group by one column and get sum of another two field

Pankaj

I have a feed collection with sample data like :

{
"_id" : ObjectId("55deb33dcb9be727e8356289"),
"userName" : "John",
"likeCount" : 2,
"commentCount" : 10,
}
,
{
"_id" : ObjectId("55deb33dcb3456e33434d456"),
"channelName" : "Mark",
"likeCount" : 5,
"commentCount" : 10,
},
{
"_id" : ObjectId("55acd3434324acd3e4567567"),
"userName" : "John",
"likeCount" : 12,
"commentCount" : 15,
}

I want to get all the records with group by "userName" and sum of "likeCount" + "commentCout". In Mysql we use :

select userName,sum(likeCount)+sum(commentCount) as "totalCount"  from feed group by userName

How can I write the aggregation for above query?

chridam

I believe this can be achieved by adding an extra field that adds the likeCount and commentCount fields in the $project stage, then do the sum operation on that field for documents grouped by the userName key. Something like this in mongo query:

db.feed.aggregate([
    {
        "$project": {
            "userName": 1,
            "sumLikeAndCommentCounts": {
                "$add": [ "$likeCount", "$commentCount" ]
            }
        }
    },
    {
        "$group": {
            "_id": "$userName",
            "totalCount": {
                "$sum": "$sumLikeAndCommentCounts"
            }
        }
    }
])

OR

you just have one pipeline step $group where you can plug in the addition operation as an expression for the $sum:

db.feed.aggregate([    
    {
        "$group": {
            "_id": "$userName",
            "totalCount": {
                "$sum": {
                    "$add": [ "$likeCount", "$commentCount" ]
                }
            }
        }
    }
])

Both of the above operation pipelines will yield the result (for the given sample data):

{ "_id" : null, "totalCount" : 15 }
{ "_id" : "John", "totalCount" : 39 }

The Spring Data MongoDB aggregation equivalent is based on the first example, with an option of using the SpEL andExpression in the projection operation:

Aggregation agg = Aggregation.newAggregation( 
    project("id", "userName") 
        .and("likeCount").plus("commentCount").as("sumLikeAndCommentCounts"),
        //.andExpression("likeCount + commentCount").as("sumLikeAndCommentCounts"), <-- or use expressions
    group("userName").sum("sumLikeAndCommentCounts").as("totalCount")
); 
AggregationResults<FeedsTotal> result = 
mongos.aggregate(agg, this.getCollectionName(), FeedsTotal.class);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Group by a column and then get the sum of another column in Rails

From Dev

Group by a column and then get the sum of another column in Rails

From Dev

Sum of two columns with join and group by another column

From Dev

Mongodb select all fields group by one field and sort by another field

From Dev

How to sum values of one column and group them by another column

From Dev

How to sum values of one column and group them by another column

From Dev

Group by one column but sum two others and count a third column

From Dev

How to group datatable by unknown column names and calculate sum of one field?

From Dev

Creating NHibernate Queryover to get sum of two field product with group by

From Java

groupby based on one column and get the sum values in another column

From Dev

Get the sum of like values in one column for each value in another column

From Dev

R - get sum from one column based on categories in another column

From Dev

Set two values in one table based on sum of a column in another table

From Dev

Group by STRING and sum another field as you group

From Dev

Group by one column and sum the other

From Dev

pandas get minimum of one column in group when groupby another

From Dev

Group By a Column and Sum contents of another column with Python

From Dev

MySQL query to sum one column and count another column, from two tables, based on a common value?

From Dev

How to suppress field and sum based on another column?

From Dev

mongodb/meteor: how to I get the value of one field corresponding to the $max value of another field?

From Dev

Group by duplicates by one column, but showing another column

From Dev

Group by duplicates by one column, but showing another column

From Dev

Sum of two columns by id to one field

From Dev

MySQL SUM over a virtual subquery field (with another SUM) with GROUP BY

From Dev

SQLSRV: group by one column and sum by month

From Dev

Get count of row and sum group by quarter of date, if another column doesnt exits a value in SQL Server

From Dev

Unix AWK field seperator finding sum of one field group by other

From Dev

query that subtracts sum of one column from another

From Dev

query that subtracts sum of one column from another

Related Related

  1. 1

    Group by a column and then get the sum of another column in Rails

  2. 2

    Group by a column and then get the sum of another column in Rails

  3. 3

    Sum of two columns with join and group by another column

  4. 4

    Mongodb select all fields group by one field and sort by another field

  5. 5

    How to sum values of one column and group them by another column

  6. 6

    How to sum values of one column and group them by another column

  7. 7

    Group by one column but sum two others and count a third column

  8. 8

    How to group datatable by unknown column names and calculate sum of one field?

  9. 9

    Creating NHibernate Queryover to get sum of two field product with group by

  10. 10

    groupby based on one column and get the sum values in another column

  11. 11

    Get the sum of like values in one column for each value in another column

  12. 12

    R - get sum from one column based on categories in another column

  13. 13

    Set two values in one table based on sum of a column in another table

  14. 14

    Group by STRING and sum another field as you group

  15. 15

    Group by one column and sum the other

  16. 16

    pandas get minimum of one column in group when groupby another

  17. 17

    Group By a Column and Sum contents of another column with Python

  18. 18

    MySQL query to sum one column and count another column, from two tables, based on a common value?

  19. 19

    How to suppress field and sum based on another column?

  20. 20

    mongodb/meteor: how to I get the value of one field corresponding to the $max value of another field?

  21. 21

    Group by duplicates by one column, but showing another column

  22. 22

    Group by duplicates by one column, but showing another column

  23. 23

    Sum of two columns by id to one field

  24. 24

    MySQL SUM over a virtual subquery field (with another SUM) with GROUP BY

  25. 25

    SQLSRV: group by one column and sum by month

  26. 26

    Get count of row and sum group by quarter of date, if another column doesnt exits a value in SQL Server

  27. 27

    Unix AWK field seperator finding sum of one field group by other

  28. 28

    query that subtracts sum of one column from another

  29. 29

    query that subtracts sum of one column from another

HotTag

Archive