Hi, I have collection like {“stringdouble”: “26.58”, “stringouble”: “36.58”, “stringdouble”: “”}
I need to convert stringdouble datatype to float in aggregate pipeline whose result looks like {“stringdouble”: 26.58, “stringdouble”: 36.58, "stringdouble: 0}, if it has “”, then it should be 0.
If you can, insert the data with the correct type in the first place.
Using $toDouble
on ""
would error so you have to use $convert
with the onError
option:
db.baz.aggregate([
{$set:{
n:{$convert: {input:'$stringdouble',to:'double',onError:0}}}},
{$project:{_id:0}}
])
[
{ stringdouble: '26.58', n: 26.58 },
{ stringdouble: '36.58', n: 36.58 },
{ stringdouble: '', n: 0 }
]