title: MongoDB string to int
body: <p>I want to convert the string of a MongoDB value field to integers and then calculate the average.</p>
  
  <p>This is my JSON:</p>
  
  <pre><code>"_id" : ObjectId("5c49f398fc0078178c76705b"),     // my json data
      "Time_Created" : ISODate("2019-01-24T17:19:20.205Z"),   // date
      "Test_ID" : "1",
      "data" : [
          {
              "Device_id" : "1",
              "Total_wires" : "5",
              "Cables" : [
                  {
                      "TAG" : "4001",
                      "Value" : "24.3"
                  },
                  {
                      "TAG" : "4002",
                      "Value" : "21.3"
                  },
                  {
                      "TAG" : "4003",
                      "Value" : "21.3"
                  },
                  {
                      "TAG" : "4004",
                      "Value" : "21.3"
                  },                                                            
                  {
                      "TAG" : "4005",                                        
                      "Value" : "100.3"
                  }
              ]
          }
      ]
  }
  </code></pre>
  
  <p>I am using the following query to extract average values</p>
  
  <ol>
  <li><p>Query:</p>
  
  <pre><code>db.collection_name.aggregate( [{  '$project': { 'values': '$data.Cables.Value', }}, { '$unwind': '$values' }, { '$addFields': { 'avgValue': { '$avg': { $toInt: '$values' } }  } } ] )
  </code></pre>
  
  <p>But I get this error:</p>
  
  <blockquote>
    <p>2019-01-29T11:06:05.642-0800 E QUERY    [js] Error: command failed:<br>
    {<br>
            "ok" : 0,<br>
            "errmsg" : "Unsupported conversion from array to int in $convert<br>
             with no onError value",<br>
            "code" : 241,<br>
            "codeName" : "ConversionFailure"<br>
    } : aggregate failed :<br>
    _getErrorWithCode@src/mongo/shell/utils.js:25:13<br>
    doassert@src/mongo/shell/assert.js:18:14<br>
    _assertCommandWorked@src/mongo/shell/assert.js:534:17<br>
    assert.commandWorked@src/mongo/shell/assert.js:618:16<br>
    DB.prototype._runAggregate@src/mongo/shell/db.js:260:9<br>
    DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1062:12<br>
    @(shell):1:1  </p>
  </blockquote></li>
  <li><p>I also tried the following:</p>
  
  <pre><code>db.collection_name.aggregate( 
  [{  '$project': { 'values': '$data.Cables.Value', }}, 
    { '$unwind': '$values' }, 
    { '$addFields': { 'avgValue': { '$avg': { 'input': '$values', 'to': 
    'int' } }  } } 
  ] )
  </code></pre>
  
  <p>But I'm also getting this output:</p>
  
  <blockquote>
    <p>{ "_id" : ObjectId("5c509604fc007808c427edcb"), "values" :<br>
    [ "24.3", "23.3", "25.3", "31.3", "90.3" ], "avgValue" : null 
    }</p>
  </blockquote></li>
  </ol>
  
  <p>The expected value should be the average of the values fields.<br>
  I need some kind of conversion for nested document values.</p>
  
  <p>The incoming string value has to be converted into integers or decimals and then the average of it should be calculated.</p>
  
  <p>This is then used in C# code to generate the pipeline </p>
  
  <pre><code>var pipeline = new[] 
  { 
    project, unwind, addfields 
  };
  </code></pre>