MongoDB $divide Operator
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, with practical examples and a detailed explanation of its capabilities.
What is 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. Here, in this operator, the arguments are 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.
Syntax:
{ $divide: [ <expression1>, <expression2> ] }
Parameters:
- Expression1: The dividend (the number to be divided).
- Expression2: The divisor (the number by which the dividend will be divided).
Both expressions must resolve to valid numeric values (integers or floats). The operator returns the result of dividing Expression1
by Expression2
Key Features of the MongoDB $divide
Operator
- Arithmetic Precision: The
$divide
operator is highly efficient for performing division in the database itself, avoiding the need for manual calculations in our application. - Flexible Data Types: It can be used with numeric fields, constants, and even more complex expressions within MongoDB queries.
- Aggregation Framework: The
$divide
operator is a part of the aggregation pipeline, enabling developers to perform complex data transformations and calculations directly in the database. - Efficient Query Execution: Since the operation is carried out on the server side, it is optimized for large datasets, reducing computational overhead on the client-side
Examples of MongoDB $divide Operator
Let's dive into some practical examples of how to use the MongoDB $divide
operator in your aggregation pipeline. 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.

Example 1: Divide using $divide operator
In this example, we use the $divide
operator to divide the salary of employees in the "Development" department by 2. This could be used, for example, to calculate a bonus or a tax deduction.
Query:
db.employee.aggregate([{$match: {department: "Development"}},
... {$project: {name: 1, halfSalary: {$divide: ["$salary", 2]}}}])
Output:

Explanation:
- The query filters the documents to include only employees in the "Development" department.
- It then divides the
salary
field by 2 using the$divide
operator. - The result of this division is projected as a new field,
halfSalary
.
Example 2: Perform Divide in the Embedded Document
The $divide
operator can be used with values stored in embedded documents. In this example, we are going to divide the salary of the HR department's employees into half, where the salary is part of an embedded document.
Query:
db.employee.aggregate([
{ $match: { department: "HR" } },
{ $project: { name: 1, halfsalary: { $divide: ["$details.salary", 2] } } }
])
Output:

Explanation:
- The query filters employees in the "HR" department.
- It divides the
salary
field inside the embeddeddetails
document by 2 using the$divide
operator. - The result is projected as a new field,
halfSalary
.
Conclusion
The $divide
operator in MongoDB is an essential tool for performing arithmetic division within our aggregation pipelines. Whether we're dividing numeric fields, constants, or expressions, this operator simplifies and accelerates calculations directly within MongoDB. By understanding this operator, we can simplify and enhance our data transformation processes, making it easier to perform calculations and derive meaningful insights from our data.