adding integer field in all document

149 views
Skip to first unread message

Suk Khoi

unread,
Jan 10, 2016, 2:44:34 AM1/10/16
to mongodb-user
when i do this
db.myCollection.update({}, {$set: {"isOpen": 0}}, false, true)

it inserts 0.0000000

i want it to insert just 0

how can i do that


Kevin Adistambha

unread,
Jan 10, 2016, 11:47:01 PM1/10/16
to mongodb-user

Hello,

db.myCollection.update({}, {$set: {“isOpen”: 0}}, false, true)

By default, the mongo shell treats all numbers as floating point values. you can force it to treat a number as an integer if you modify the update command as such:

db.coll.update({}, {$set: {"isOpen": NumberInt(0)}}, false, true)

It looks like the field name that you used (isOpen) refers to a Boolean value. If that’s the case, you might save some storage space by using a Boolean value instead, as explained in this post: How should I store boolean values in MongoDB?

What version of MongoDB are you using? From version 2.2 onwards, positional Boolean parameters in the update() are deprecated. Current versions replaced the positional parameters with an update options document instead.

Best regards,
Kevin

Reply all
Reply to author
Forward
0 new messages