Introduction to MongoDB $sum Operator

The MongoDB $sum operator is a very useful aggregation operator that can be used to perform sum operations on specified fields. In this article, we will introduce the $sum operator in terms of syntax, usage scenarios, examples, and conclusions.

Syntax

The syntax of the $sum operator is very simple, and can be used in aggregation pipelines:

$sum: <expression>

Here, <expression> represents the field or expression that needs to be summed. For example, if you want to sum the score field in a collection, you can write:

db.collection.aggregate([
  { $group: { _id: "$name", totalScore: { $sum: "$score" } } }
])

This will sum the score field for each name field and save the result in the totalScore field.

Usage Scenarios

The $sum operator can be widely used in various aggregation scenarios, especially for numerical calculations on specified fields. For example, you can use the $sum operator to calculate the sum of various fields in a collection, or to calculate the average, maximum, and minimum values of a field.

Examples

We will introduce three examples of the $sum operator:

Example 1: Summation

Assuming that there is a score sheet as follows:

{ name: "Alice", score: 85 }
{ name: "Lucy", score: 92 }
{ name: "James", score: 78 }
{ name: "Kobe", score: 90 }

Now, if we want to sum the score field in this score sheet, we can use the following aggregation pipeline:

db.scores.aggregate([{ $group: { _id: null, totalScore: { $sum: "$score" } } }])

After running, the following result will be obtained:

{ "_id" : null, "totalScore" : 345 }

Example 2: Group Summation

In Example 1, we summed the entire collection. Now, assuming that we want to sum the scores of each student, we can use the following aggregation pipeline:

db.scores.aggregate([
  { $group: { _id: "$name", totalScore: { $sum: "$score" } } }
])

After running, the following result will be obtained:

{ "_id" : "Alice", "totalScore" : 85 }
{ "_id" : "Lucy", "totalScore" : 92 }
{ "_id" : "James", "totalScore" : 78 }
{ "_id" : "Kobe", "totalScore" : 90 }

Example 3: Conditional Summation

In some cases, we may need to perform summation operations on documents that meet specific conditions. For example, assuming that we only want to calculate the total score of students who score greater than or equal to 90, we can use the following aggregation pipeline:

db.scores.aggregate([
  { $match: { score: { $gte: 90 } } },
  { $group: { _id: null, totalScore: { $sum: "$score" } } }
])

After running, the following result will be obtained:

{ "_id" : null, "totalScore" : 182 }

Here, the $match operator is used to filter out documents with a score greater than or equal to 90, and then the $group operator is used to perform summation operations.

Conclusion

Through the introduction in this article, we have learned about the syntax, usage scenarios, and examples of the $sum operator. In general, the $sum operator is a very useful aggregation operator, which can conveniently perform summation operations on specified fields and is applicable to various aggregation scenarios. It should be noted that if you want to sum multiple fields, you can use expressions to perform calculations within the $sum operator.