
- 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
Is it possible to sum two fields in MongoDB using the Aggregation framework?
Yes, it is possible using the $project operator. Let us first create a collection with documents
> db.sumTwoFieldsDemo.insertOne({"FirstValue":150,"SecondValue":350}); { "acknowledged" : true, "insertedId" : ObjectId("5c9b4bfe15e86fd1496b38cd") } > db.sumTwoFieldsDemo.insertOne({"FirstValue":450,"SecondValue":1550}); { "acknowledged" : true, "insertedId" : ObjectId("5c9b4c1215e86fd1496b38ce") } > db.sumTwoFieldsDemo.insertOne({"FirstValue":2560,"SecondValue":2440}); { "acknowledged" : true, "insertedId" : ObjectId("5c9b4c2715e86fd1496b38cf") }
Following is the query to display all documents from a collection with the help of find() method
> db.sumTwoFieldsDemo.find().pretty();
This will produce the following output
{ "_id" : ObjectId("5c9b4bfe15e86fd1496b38cd"), "FirstValue" : 150, "SecondValue" : 350 } { "_id" : ObjectId("5c9b4c1215e86fd1496b38ce"), "FirstValue" : 450, "SecondValue" : 1550 } { "_id" : ObjectId("5c9b4c2715e86fd1496b38cf"), "FirstValue" : 2560, "SecondValue" : 2440 }
Following is the query to sum 2 fields in MongoDB using the $project operator
> db.sumTwoFieldsDemo.aggregate( ... { "$project" : ... { ... 'First' : '$FirstValue', ... 'Second' : '$SecondValue', ... 'TotalValueOfBothFields' : { '$add' : [ '$FirstValue', '$SecondValue' ] }, ... } ... } ... );
This will produce the following output
{ "_id" : ObjectId("5c9b4bfe15e86fd1496b38cd"), "First" : 150, "Second" : 350, "TotalValueOfBothFields" : 500 } { "_id" : ObjectId("5c9b4c1215e86fd1496b38ce"), "First" : 450, "Second" : 1550, "TotalValueOfBothFields" : 2000 } { "_id" : ObjectId("5c9b4c2715e86fd1496b38cf"), "First" : 2560, "Second" : 2440, "TotalValueOfBothFields" : 5000 }
- Related Articles
- MongoDB query to group several fields using aggregation framework?
- Match between fields in MongoDB aggregation framework?
- MongoDB aggregation framework match OR is possible?
- How to exclude _id without including other fields using the aggregation framework in MongoDB?
- How to compare two fields in aggregation filter with MongoDB?
- Is it possible to rename _id field after MongoDB group aggregation?
- Is it possible to exclude nested fields in MongoDB with a wildcard?
- Get the average of an entire field using the aggregation framework in MongoDB?
- Get Absolute value with MongoDB aggregation framework?
- MongoDB aggregation framework with group query example?
- MongoDB aggregation framework to sort by length of array?
- MongoDB order by two fields sum?
- MongoDB query (aggregation framework) to match a specific field value
- Count by multiple fields with MongoDB aggregation
- MongoDB aggregation to combine or merge fields and then count?

Advertisements