- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to get a “-Infinity” result for $avg in an aggregate query?
For this, you can use aggregate(). Let us first create a collection with documents with a vlaue as -infinity −
> db.demo5.insertOne({ "_id" : 100, "seq" : 10, "Value" : -Infinity }); { "acknowledged" : true, "insertedId" : 100 } > db.demo5.insertOne({ "_id" : 101, "seq" : 10, "Value" : 50 }); { "acknowledged" : true, "insertedId" : 101 } > db.demo5.insertOne({ "_id" : 102, "seq" : 20, "Value" : 60 }); { "acknowledged" : true, "insertedId" : 102 } > db.demo5.insertOne({ "_id" : 103, "seq" : 20, "Value" : 50 }); { "acknowledged" : true, "insertedId" : 103 }
Following is the query to display all documents from a collection with the help of find() method −
> db.demo5.find();
This will produce the following output &miuns;
{ "_id" : 100, "seq" : 10, "Value" : -Infinity } { "_id" : 101, "seq" : 10, "Value" : 50 } { "_id" : 102, "seq" : 20, "Value" : 60 } { "_id" : 103, "seq" : 20, "Value" : 50 }
Here is the query to get a “-Infinity” result for $avg in an aggregate query. On the basis of seq, we have found the average. This results in -Infinity −
> db.demo5.aggregate([{$group:{"_id":"$seq", "average" : {$avg : "$Value"}}}]);
This will produce the following output −
{ "_id" : 20, "average" : 55 } { "_id" : 10, "average" : -Infinity }
Advertisements