- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MongoDB divide aggregation operator?
You can use aggregate framework for this. Let us first create a collection with documents −
>db.aggregationOperatorDemo.insertOne({"FirstValue":392883,"SecondValue":10000000000}); { "acknowledged" : true, "insertedId" : ObjectId("5cd541452cba06f46efe9f01") }
Following is the query to display all documents from a collection with the help of find() method −
> db.aggregationOperatorDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd541452cba06f46efe9f01"), "FirstValue" : 392883, "SecondValue" : 10000000000 }
Following is the query to use divide aggregation operator −
> db.aggregationOperatorDemo.aggregate([ ... { "$project": { "Value": { "$divide": ["$FirstValue", "$SecondValue"] } } } ... ]);
This will produce the following output −
{ "_id" : ObjectId("5cd541452cba06f46efe9f01"), "Value" : 0.0000392883 }
Advertisements