1

The original data is:

{"u":1}

The type of 'u' is Int. After I run command in mongod console:

update({},{$set:{"u":0}})
find({})

data indeed become

{"u":0}

looks pretty ok. But when I use C++ driver to read them:

bson.getIntField("u")

Crash. The reason is type 'u' is Double! That means mongod's update command change u's type soundlessly.

Why? and how to prevent this?

P.S mongodb version is 2.6.6 linux

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
jean
  • 2,825
  • 2
  • 35
  • 72

1 Answers1

2

Any numeric value is inserted or "changed" as a Double by default. For other "types" use the NumberInt() or NumberLong() for the respective type you expect to read in your C++ or other type sensitive code:

update({},{ "$set":{ "u": NumberInt(0) }})

or:

update({},{ "$set":{ "u": NumberLong("0") }})
Neil Lunn
  • 148,042
  • 36
  • 346
  • 317