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 }