DanielLu
DanielLu

Reputation: 23

How to find the average of a field in Mongodb?

I'm attempting to find the average price of books in a js file.

So far I've managed to count the number of books which totals to 4 (query shown below)

db.bookshop.aggregate([ {"$unwind":"$book"},
                          {"$project":{"price":"$book.book","_id":0}},
                          {"$count":"average price of books"} ]).pretty();


Output for this query:

{ "average price of books" : 4 }

and this is my attempt at finding the total prices of all books which does not work. I've done this as I assume to find the average it would be the sum divided by count.

db.bookshhop.aggregate([ {$group: {_id: null,"TotalAmount": 
{$sum: "$book.price"}}}] );

I'm having issues in finding the Average price of all book prices. How can I do this? Thanks

These are the book js records:

 db.bookshop.insert( {
    "_id":"185.3.16",
    "book": {
        "callnum":"185.3.16",
        "isbn":"1-292-06118-9",
        "title":"Database Systems",
        "authors":[
            {
                "fname":"Thomas",
                "lname":"Connolly"},
            { 
                "fname":"Carolyn",
                "lname":"Begg"}
        ],
        "publisher":"Pearson Pty Ltd",
        "year":2015,
        "price":136.99,
        "topic":"Computer Science",
        "description":"This is the 6th edition. You can register online to access the examples",
        "keywords":["Database", "XML", "Distributed"]
        }
});

db.bookshop.insert( {
    "_id":"163.24.12",
    "book": {
        "callnum":"163.24.12",
        "isbn":"1-123-456-810",
        "title":"Core Java",
        "authors":[
            {
                "fname":"Horstmann",
                "lname":"Cornell"}
        ],
        "publisher":"PH Pty Ltd",
        "year":2012,
        "price":142.90,
        "topic":"Computer Science",
        "description":"It covers JAVA programming and JAVA script",
        "keywords":["JAVA", "XML", "Script"]
    }
});

db.bookshop.insert( {
    "_id":"123.45.67",
    "book": {
        "callnum":"123.45.67",
        "isbn":"1-123-456-789",
        "title":"Algorithms",
        "authors":[
            {
                "fname":"James",
                "lname":"Bond"},
            {
                "fname":"Harry",
                "lname":"Potter"},
            {
                "fname":"William",
                "lname":"Stallings"}
        ],
        "publisher":"Pearson Pty Ltd",
        "year":2013,
        "price":65.85,
        "topic":"Computer Science",
        "description":"It contains algorithms and their applications. You can download examples from the website"
    }
});
db.bookshop.insert( {
    "_id":"134.41.33",
    "book": {
        "callnum":"134.41.33",
        "isbn":"1-213-431-770",
        "title":"C++ Programming",
        "authors":[
            {
                "fname":"Larry",
                "lname":"Peterson"}
        ],
        "publisher":"Pearson Pty Ltd",
        "year":2010,
        "price":74.90,
        "topic":"Computer Science",
        "description":"C++ programming and its applications",
        "keywords":["C++", "Class", "Overloading", "Inheritance"]
    }
});


Upvotes: 1

Views: 225

Answers (2)

Dexter
Dexter

Reputation: 982

db.collection.aggregate( [{ $group: {  _id: "Books Price", "average": {    "$avg": "$book.price"  }} }] )

Upvotes: 0

SuleymanSah
SuleymanSah

Reputation: 17888

You can get the average of price using $avg.

docs: https://docs.mongodb.com/manual/reference/operator/aggregation/avg

db.bookshop.aggregate([
  {
    $group: {
      _id: null,
      AvgPrice: {
        $avg: "$book.price"
      }
    }
  }
]);

This will give the following result:

[
  {
    "AvgPrice": 105.16,
    "_id": null
  }
]

Here is a playground to test. https://mongoplayground.net/p/csfgxFBbYfx

Upvotes: 2

Related Questions