
- 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 sum the value of a key across all documents in a MongoDB collection?
To get sum the value of a key across all documents in a MongoDB collection, you can use aggregate().
To understand the above concept, let us create a collection with the document. The query to create a collection with a document is as follows −
> db.sumOfValueDemo.insertOne({"Name":"Larry","Amount":14.50}); { "acknowledged" : true, "insertedId" : ObjectId("5c8ee7272f684a30fbdfd592") } > db.sumOfValueDemo.insertOne({"Name":"Mike","Amount":15.68}); { "acknowledged" : true, "insertedId" : ObjectId("5c8ee7342f684a30fbdfd593") } > db.sumOfValueDemo.insertOne({"Name":"Carol","Amount":50.32}); { "acknowledged" : true, "insertedId" : ObjectId("5c8ee7412f684a30fbdfd594") } > db.sumOfValueDemo.insertOne({"Name":"David","Amount":120.90}); { "acknowledged" : true, "insertedId" : ObjectId("5c8ee7532f684a30fbdfd595") }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.sumOfValueDemo.find().pretty();
The following is the output −
{ "_id" : ObjectId("5c8ee7272f684a30fbdfd592"), "Name" : "Larry", "Amount" : 14.5 } { "_id" : ObjectId("5c8ee7342f684a30fbdfd593"), "Name" : "Mike", "Amount" : 15.68 } { "_id" : ObjectId("5c8ee7412f684a30fbdfd594"), "Name" : "Carol", "Amount" : 50.32 } { "_id" : ObjectId("5c8ee7532f684a30fbdfd595"), "Name" : "David", "Amount" : 120.9 }
Here is the query to sum the value of a key across all documents in a MongoDB collection:
> db.sumOfValueDemo.aggregate({ ... $group: { ... _id: '', ... "Amount": { $sum: '$Amount' } ... } ... }, { ... $project: { ... _id: 0, ... "TotalAmount": '$Amount' ... } ... });
The following is the output:
{ "TotalAmount" : 201.4 }
- Related Articles
- Find all duplicate documents in a MongoDB collection by a key field?
- How to delete all the documents from a collection in MongoDB?
- How to add a new field to all the documents in a MongoDB collection
- How to retrieve all the documents from a MongoDB collection using Java?
- How to remove all documents from a collection except a single document in MongoDB?
- How to count the number of documents in a MongoDB collection?
- Fetching all documents from MongoDB Collection in a beautified form
- How to read a specific key-value pair from a MongoDB collection?
- Find a value in lowercase from a MongoDB collection with documents
- How to delete documents from a collection in MongoDB?
- How to retrieve documents from a collection in MongoDB?
- Display only a single field from all the documents in a MongoDB collection
- How to sort the documents of a MongoDB collection using java?
- How to return documents of a collection without objectId in MongoDB?
- Get all embedded documents with “isMarried” status in a MongoDB collection

Advertisements