CONVERT NULL data type to decimal data type in mongodb compass

Viewed 11

I am trying to convert a field datatype from null to decimal.

I tried to covert in the following way:

db.Battery.update(
  {MAX_AH_DAILY_THROUGHPUT:{$type:10}},
  [{$set:{MAX_AH_DAILY_THROUGHPUT:{$toDecimal:"$MAX_AH_DAILY_THROUGHPUT"}}}],
  {multi:true}
);

In MAX_AH_DAILY_THROUGHPUT field all values are null, but as per requirement I need to convert the field type as decimal.

Can someone help me how to convert it into decimal.

Thanks in advance.

1 Answers

If the current value is null, you don't need to use it, just use:

db.Battery.update(
  {MAX_AH_DAILY_THROUGHPUT: {$type: 10}}, 
  [{$set: {MAX_AH_DAILY_THROUGHPUT: {$toDecimal: 0}}}],
  {multi:true})
Related