
- 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 calculate average of a particular field in MongoDB?
To calculate average of a particular field, use aggregate() along with $avg. Let us create a collection with documents −
> db.demo214.insertOne({"Marks":56,"Name":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3e319403d395bdc2134705") } > db.demo214.insertOne({"Marks":86,"Name":"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3e319c03d395bdc2134706") } > db.demo214.insertOne({"Marks":78,"Name":"Carol"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3e31a403d395bdc2134707") }
Display all documents from a collection with the help of find() method −
> db.demo214.find();
This will produce the following output −
{ "_id" : ObjectId("5e3e319403d395bdc2134705"), "Marks" : 56, "Name" : "David" } { "_id" : ObjectId("5e3e319c03d395bdc2134706"), "Marks" : 86, "Name" : "Bob" } { "_id" : ObjectId("5e3e31a403d395bdc2134707"), "Marks" : 78, "Name" : "Carol" }
Following is the query to calculate average of a particular field in MongoDB −
> db.demo214.aggregate( ... [ ... { ... $group: ... { ... ... "_id":"_id", ... AverageValue: { $avg: "$Marks" } ... } ... } ... ] ...)
This will produce the following output
{ "_id" : "_id", "AverageValue" : 73.33333333333333 }
- Related Articles
- MongoDB query to calculate average
- Calculate average of ratings in array and then include the field to original document in MongoDB?
- Calculate average value in MongoDB
- Particular field as result in MongoDB?
- In MySQL how to replace all NULL values in a particular field of a particular table?
- MongoDB query to fetch a document that does not have a particular field?
- Calculate the average value in a MongoDB document grouping by null?
- How to find the average of a particular column in R data frame?
- How to increment a field in MongoDB?
- Get the average of an entire field using the aggregation framework in MongoDB?
- How to calculate Arithmetic Average Return?
- How to return only value of a field in MongoDB?
- How to change the type of a field in MongoDB?
- How to calculate the average rate of change in Excel?
- How to delete particular data in a document in MongoDB?

Advertisements