In MongoDB, the $divide
operator is a powerful tool used to perform division between two numerical values. It allows for precise arithmetic operations directly within the database queries, enhancing the capability to manipulate and analyze data. In this article, We will learn about the MongoDB $divide Operator by understanding various examples in detail.
MongoDB $divide operator
- MongoDB $divide operator is used to perform division between two numbers. It divides one number by another and returns the result.
- MongoDB $divide operator can also be used within the MongoDB Aggregation Framework to divide the values of fields, constants, or expressions.
Syntax:
{ $divide: [ <expression1>, <expression2> ] }
Here, in this operator, the arguments passed in an array. And the first argument is a dividend and the second argument is a divisor. The argument must be a valid expression until it resolves to a number.
Examples of MongoDB $divide operator
In the following examples, we are working with:
Database: GeeksforGeeks
Collection: employee
Document: three documents that contain the details of the employees in the form of field-value pairs.
Output:
Example 1: Divide using $divide operator
In this example, we are going to divide the salary of the development department’s employees into half.
db.employee.aggregate([{$match: {department: "Development"}},
... {$project: {name: 1, halfSalary: {$divide: ["$salary", 2]}}}])
Output:
Explanation:This query filters the documents to include only those in the “Development” department, and then projects each employee’s name and half of their salary using the $divide
operator.
Example 2: Perform Divide in the Embedded Document
In this example, we are going to divide the salary of the HR department’s employees into half.
db.employee.aggregate([
{ $match: { department: "HR" } },
{ $project: { name: 1, halfsalary: { $divide: ["$details.salary", 2] } } }
])
Output:
Explanation: This query filters documents to include only those in the “HR” department, then projects the employee’s name and half of their salary using the $divide
operator.
Conclusion
The $divide
operator in MongoDB is an essential tool for performing arithmetic division within your aggregation pipelines. By understanding this operator, you can simplify and enhance your data transformation processes, making it easier to perform calculations and derive meaningful insights from your data.
FAQs on MongoDB $divide
Can the $divide
operator handle division by zero?
No, dividing by zero with the $divide
operator will result in an error. It’s important to ensure that the divisor is not zero to avoid runtime errors in your aggregation pipeline.
Can the $divide
operator be used with non-numeric values?
No, both arguments for the $divide
operator must resolve to numeric values. Using non-numeric values will result in an error.
Is it possible to use the $divide
operator with fields from embedded documents?
Yes, you can use the $divide
operator with fields from embedded documents by specifying the correct path to the field within the embedded document.
Can the $divide
operator be combined with other arithmetic operators in the same pipeline?
Yes, the $divide
operator can be combined with other arithmetic operators such as $add
, $subtract
, and $multiply
within the same aggregation pipeline to perform complex calculations.