
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
How to subtract values (TotalPrice – Discount) from document field values in MongoDB?
To subtract values from document field values, use $subtract in MongoDB aggregate(). Let us create a collection with documents −
> db.demo599.insertOne({"TotalPrice":250,"DiscountPrice":35});{ "acknowledged" : true, "insertedId" : ObjectId("5e948192f5f1e70e134e2696") } > db.demo599.insertOne({"TotalPrice":400,"DiscountPrice":10});{ "acknowledged" : true, "insertedId" : ObjectId("5e948199f5f1e70e134e2697") } > db.demo599.insertOne({"TotalPrice":1550,"DiscountPrice":50});{ "acknowledged" : true, "insertedId" : ObjectId("5e9481a0f5f1e70e134e2698") }
Display all documents from a collection with the help of find() method −
> db.demo599.find();
This will produce the following output −
{ "_id" : ObjectId("5e948192f5f1e70e134e2696"), "TotalPrice" : 250, "DiscountPrice" : 35 } { "_id" : ObjectId("5e948199f5f1e70e134e2697"), "TotalPrice" : 400, "DiscountPrice" : 10 } { "_id" : ObjectId("5e9481a0f5f1e70e134e2698"), "TotalPrice" : 1550, "DiscountPrice" : 50 }
Following is the query to subtract values from document field values −
> db.demo599.aggregate( [ { $project: {ActualPrice: { $subtract: [ "$TotalPrice", "$DiscountPrice" ] } } } ] )
This will produce the following output −
{ "_id" : ObjectId("5e948192f5f1e70e134e2696"), "ActualPrice" : 215 } { "_id" : ObjectId("5e948199f5f1e70e134e2697"), "ActualPrice" : 390 } { "_id" : ObjectId("5e9481a0f5f1e70e134e2698"), "ActualPrice" : 1500 }
- Related Articles
- How to get distinct list of sub-document field values in MongoDB?
- Filter specific values from a MongoDB document
- How to compare field values in MongoDB?
- Remove values from a matrix like document in MongoDB
- Fetch specific field values in MongoDB
- How to remove a field completely from a MongoDB document?
- Display the last two values from field with MongoDB
- Find values group by another field in MongoDB?
- How to count number of distinct values per field/ key in MongoDB?
- How to subtract column values from column means in R data frame?
- R programming to subtract all values in a vector from all values in another vector.
- Getting unique values within two arrays in one MongoDB document
- How to get unique values from MongoDB collection?
- Accessing array values in a MongoDB collection to fetch a specific document
- Get the duplicate values of a field in MongoDB?

Advertisements