
- 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
Get the average of an entire field using the aggregation framework in MongoDB?
You can use aggregate() method for this. Let us first create a collection with documents
> db.averageAggregationDemo.insertOne({"PlayerGameScore":45}); { "acknowledged" : true, "insertedId" : ObjectId("5c9ed66bd628fa4220163b95") } > db.averageAggregationDemo.insertOne({"PlayerGameScore":55}); { "acknowledged" : true, "insertedId" : ObjectId("5c9ed671d628fa4220163b96") } > db.averageAggregationDemo.insertOne({"PlayerGameScore":65}); { "acknowledged" : true, "insertedId" : ObjectId("5c9ed676d628fa4220163b97") } > db.averageAggregationDemo.insertOne({"PlayerGameScore":35}); { "acknowledged" : true, "insertedId" : ObjectId("5c9ed67bd628fa4220163b98") } > db.averageAggregationDemo.insertOne({"PlayerGameScore":16}); { "acknowledged" : true, "insertedId" : ObjectId("5c9ed701d628fa4220163b99") }
Following is the query to display all documents from a collection with the help of find() method
> db.averageAggregationDemo.find().pretty();
This will produce the following output
{ "_id" : ObjectId("5c9ed66bd628fa4220163b95"), "PlayerGameScore" : 45 } { "_id" : ObjectId("5c9ed671d628fa4220163b96"), "PlayerGameScore" : 55 } { "_id" : ObjectId("5c9ed676d628fa4220163b97"), "PlayerGameScore" : 65 } { "_id" : ObjectId("5c9ed67bd628fa4220163b98"), "PlayerGameScore" : 35 } { "_id" : ObjectId("5c9ed701d628fa4220163b99"), "PlayerGameScore" : 16 } Following is the query to get the average of an entire field with aggregation: > db.averageAggregationDemo.aggregate({ "$group": { "_id": null, "PlayerGameScoreAverage": { "$avg": "$PlayerGameScore" } } } );
This will produce the following output
{ "_id" : null, "PlayerGameScoreAverage" : 43.2 }
- Related Articles
- Get Absolute value with MongoDB aggregation framework?
- MongoDB query (aggregation framework) to match a specific field value
- MongoDB query to get average in aggregation of array element?
- MongoDB query to group several fields using aggregation framework?
- Match between fields in MongoDB aggregation framework?
- Is it possible to sum two fields in MongoDB using the Aggregation framework?
- Retrieving an embedded object as a document via the aggregation framework in MongoDB?
- How to exclude _id without including other fields using the aggregation framework in MongoDB?
- MongoDB aggregation framework match OR is possible?
- MongoDB aggregation framework with group query example?
- Aggregation framework to get the name of students with test one score less than the total average of all the tests
- MongoDB aggregation framework to sort by length of array?
- Get the average of marks in MongoDB with aggregate?
- Get the duplicate values of a field in MongoDB?
- MongoDB aggregation to get two documents with the least marks

Advertisements