MongoDB divide aggregation operator?

MongoDBDatabaseBig Data Analytics

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 }
raja
Published on 23-May-2019 12:51:07
Advertisements