Change the type of a MongoDB field to integer from its console
In the C/C++ "sense of the word", ints are not actually guaranteed to be 32-bit values. An int
must be at least 16-bits, but generally matches the platform architecture (eg. 32 or 64bit).
As mentioned by @Jasd, JavaScript does only have one numeric type which is a floating point (double
in C).
From the MongoDB shell you should be able to use the functions NumberInt(..)
to get a BSON 32-bit integer value or NumberLong(..)
to get a BSON 64-bit integer.
You can fix the types of all values for a certain field on the console with something like:
db.Statistic.find({kodoId: {$exists: true}}).forEach(function (x) { x.kodoId = NumberInt(x.kodoId); db.Statistic.save(x);});
This will only load documents that have the kodoId
field and convert them to int and resave. You could do something like:
db.Statistic.find({kodoId: {$exists: true}}, {kodoId: 1}).forEach(function (x) { db.Statistic.update({ _id: x._id }, {$set: { kodoId: NumberInt(x.kodoId) }});});
with a second param which will only load the kodoId value (hopefully from an index). This should be much quicker since it doesn't need to load and resave the entire document - just that one field.
MongoDB's shell is powered by a JavaScript Engine. And JavaScript has no type for integer
. It knows only Number
, which is a type for floating-point numbers.
You can try using the MongoDB type NumberLong
, but it didn't work out for me when i once had the same problem.