0

I tried to add two integer type columns to all the documents on one of my collections, so I used $addFields, my query is below:

db.test.aggregate([
 {
       $addFields:
       {
          "NumberOne": 0,
          "NumberTwo": 0,
       }
    }
 ]
 )

After I run the query, the two new columns were added as double type, 0.0 instead of int32 type 0. I tried to use updatemany to update the column with the following db.test.updateMany({}, {$set:{"NumberOne": 0}}) and still no help. How can I just add two new integer columns in mongodb? Thanks!

Slow Snail
  • 137
  • 2
  • 12
  • [Here] (https://stackoverflow.com/questions/8218484/mongodb-inserts-float-when-trying-to-insert-integer) an answer for update solution. – Erailea May 03 '21 at 14:32
  • Does this answer your question? [MongoDB inserts float when trying to insert integer](https://stackoverflow.com/questions/8218484/mongodb-inserts-float-when-trying-to-insert-integer) – Tom Slabbaert May 03 '21 at 14:33

1 Answers1

0

Thanks for the tips from above links. So the trick here is to use NumberInt(0) instead of 0 . So when the query should be:

db.test.aggregate([
 {
       $addFields:
       {
          "NumberOne": NumberInt(0),
          "NumberTwo": NumberInt(0),
       }
    }
 ]
 )
Slow Snail
  • 137
  • 2
  • 12