0

I have a Mongo query with a sum but the field type is a string, So I need project the string like a number, Is this possible? The aggretation code:

  {
    $project: {
      _id: 1,
      data: 1
    }
  }, {
    $group: {
      count: {
        $sum: 1
      },
      total_risk: {
        $sum: "$data.selcars"
      },
      string_value: {
         $push: {
           risksCar: "$data.selcars",               
         }
       }
     }       

This is the result:

  {
    "count" : 2,
    "total_risk" : 0,
    "string_value" : [ 
        {
            "risksCar" : "4"
        }, 
        {
            "risksCar" : "3"
        }    
    ]
  }
Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
aImazio
  • 36
  • 4
  • Can you edit your question to show us the sample document and the Mongo query you are trying to do? – chridam Apr 17 '15 at 10:46
  • Nop, I don't think so. I need project _$data.selcars_ to a number. – aImazio Apr 17 '15 at 11:30
  • @aImazio first you should changed data type of your documents. For changing data types refer this http://stackoverflow.com/questions/27498872/how-to-change-datatype-of-netsted-field-in-mongo-document then used same above your aggregation query , then you will get exact result – Neo-coder Apr 17 '15 at 12:06

0 Answers0