
- 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
MongoDB query to sum specific key values with terminal commands?
For this, use $match along with aggregate(). Let us create a collection with documents −
> db.demo329.insertOne({"Name":"Chris","Age":21,"Marks":45}); { "acknowledged" : true, "insertedId" : ObjectId("5e516f28f8647eb59e56207d") } > db.demo329.insertOne({"Name":"David","Age":21,"Marks":56}); { "acknowledged" : true, "insertedId" : ObjectId("5e516f28f8647eb59e56207e") } > db.demo329.insertOne({"Name":"Mike","Age":21,"Marks":78}); { "acknowledged" : true, "insertedId" : ObjectId("5e516f29f8647eb59e56207f") } > db.demo329.insertOne({"Name":"David","Age":21,"Marks":89}); { "acknowledged" : true, "insertedId" : ObjectId("5e516f29f8647eb59e562080") }
Display all documents from a collection with the help of find() method −
> db.demo329.find().pretty();
This will produce the following output:
{ "_id" : ObjectId("5e516f28f8647eb59e56207d"), "Name" : "Chris", "Age" : 21, "Marks" : 45 } { "_id" : ObjectId("5e516f28f8647eb59e56207e"), "Name" : "David", "Age" : 21, "Marks" : 56 } { "_id" : ObjectId("5e516f29f8647eb59e56207f"), "Name" : "Mike", "Age" : 21, "Marks" : 78 } { "_id" : ObjectId("5e516f29f8647eb59e562080"), "Name" : "David", "Age" : 21, "Marks" : 89 }
Following is the query to sum key values −
> db.demo329.aggregate([ ... { ... $match: { ... $and: [ ... { 'Name': 'David'}, ... { Age:21} .. ] ... } ... }, ... { ... $group: { ... _id: null, ... TotalMarks: { $sum: "$Marks" } ... } ... } ... ])
This will produce the following output −
{ "_id" : null, "TotalMarks" : 145 }
- Related Articles
- MongoDB query to sum specific fields
- MongoDB query to match documents with array values greater than a specific value
- How to query MongoDB a value with $lte, $in and $not to fetch specific values?
- MongoDB query to find documents with specific FirstName and LastName
- MongoDB query to add matched key to list after query?
- MongoDB query to 'sort' and display a specific number of values
- MongoDB query to add up the values of a specific field in documents
- Update object in array with a specific key in MongoDB
- How to filter a query on specific date format with MongoDB?
- MongoDB query to remove a specific document
- How to query a key having space in its name with MongoDB?
- Is it possible to return a list of specific values from a MongoDB query?
- MongoDB query to concatenate values of array with other fields
- MongoDB query to get record beginning with specific element from an array?
- MongoDB query to display documents with a specific name irrespective of case

Advertisements