Mongo Aggregation : Double Array with NaN values - $sum , $avg,$min returns NaN results but not $max

507 views
Skip to first unread message

Kowshik Nandagudi

unread,
Dec 19, 2016, 10:49:52 AM12/19/16
to mongodb-user
Hi All,

I have document in the similar structure to hold a 5 minutely values in a document.


Document structure:


    "_id" : ObjectId("5857f58d396adf19c41a3f14"), 
    "source" : "LONDON.CITY.ALL", 
    ""uIndex" : ISODate("2016-01-01T00:00:00.000+0000"), 
    "values" : {
        "0" : [NaN, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0], 
        "1" : [13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0],
.....
....
"23" : [277.0, 278.0, 279.0, 280.0, 281.0, 282.0, 283.0, 284.0, 285.0, 286.0, 287.0, 288.0      ]
      }
}



I am doing an hourly aggregation to find min , max, average, sum per hour . One of the values is NaN. 

Aggregation returns the following results



Aggregation Query :
db.City.aggregate([
                                                     {
                                                       $match:{"source":LONDON.CITY.ALL"}
                                                     },
                                                     
                                                    
                                                    {$unwind:"$values.0"}, 
                                          
                                                    {$group : { _id :"$source", sum:{$sum:"$values.0"},avg:{$avg:"$values.0"},max:{$max:"$values.0"},min:{$min:"$values.0"}}}
                                                    
                                                                                     
                                                ])


Aggregation results:

Sum: NaN
Avg:NaN
Max: 12 
Min:Nan


Question is : 

1. Why is Sum , average ,Min operators returning Nan but Max is returning the maximum of that array    "0" : [NaN, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0] ?
2. How do we handle NaN in case of a double array


Thanks in Advance

Kowshik NS

Kowshik Nandagudi

unread,
Dec 20, 2016, 7:35:45 AM12/20/16
to mongodb-user
I have raised a ticket in Mongo DB. Here is the reference.

Reply all
Reply to author
Forward
0 new messages