Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (50.2k points)

In MongoDB, I am having a collection named foo:

{ "_id" : ObjectId("5837199bcabfd020514c0bae"), "x" : 1 }

{ "_id" : ObjectId("583719a1cabfd020514c0baf"), "x" : 3 }

{ "_id" : ObjectId("583719a6cabfd020514c0bb0") }

I implemented the following query:

db.foo.aggregate({$group:{_id:1, avg:{$avg:"$x"}, sum:{$sum:1}}})

The output is:

{ "_id" : 1, "avg" : 2, "sum" : 3 }

I want to know the meaning of {$sum:1} in the query.

1 Answer

0 votes
by (107k points)

In MongoDB, the $sum will add up the value of the expression for every row and in your case, you are having 3 rows so it will be 1+1+1 =3.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Feb 5, 2020 in Web Technology by ashely (50.2k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...