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 }

Updated on: 30-Jul-2019

198 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements