String to Int conversion results in Null once condition is added

I have an aggregation pipeline that successfully converts a string numeric values to Int:

{ $addFields: { IncomeC: { $toInt: “$Income” }

However once a condition is added it returns “Null” for all values:

{ $addFields: {
  IncomeF: { $toInt: {$cond: { if: {$ne: [ "$Income", "" ] }, then: "$Income", else: 0 } } }

$cond without conversion returns correct numeric values so the problem must be with $toint.

Would much appreciate any hints.

Try this:

{ 
  $addFields: {
    IncomeF: { 
      $cond: { 
        if: { $ne: [ "$Income", "" ] },
        then:  { $toInt: "$Income" }, 
        else: 0 
    } 
  } 
}

@Victor_Mudretsov what version of MongoDB are you using and how are you querying this data (shell, driver, other)? Can you share a sample document that is returning a correct value without the condition so I can test, because my tests appear to produce the desired result:

use('test');


db.foo.drop();
db.foo.insertOne({ Income: "123" });
db.foo.insertOne({ Income: "" });
db.foo.aggregate([
  { $addFields: {
    IncomeF: { $toInt: {$cond: { if: {$ne: [ "$Income", "" ] }, then: "$Income", else: 0 } } }
  }}
]);
// output
[
  {
    "_id": {
      "$oid": "6369591efeaec7258dbf1821"
    },
    "Income": "123",
    "IncomeF": 123
  },
  {
    "_id": {
      "$oid": "6369591efeaec7258dbf1822"
    },
    "Income": "",
    "IncomeF": 0
  }
]

Thanks a lot for your responses, I have sorted it by moving things around a little.