Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the type of a MongoDB field to integer from its console

We have an application that stores some configuration values from C/C++ in MongoDB and has the capability to be restarted (i.e. it runs for a while, someone interrupts the application, changes the configuration, then runs the app again, and it picks up where it left off). This works like a charm for boolean and string configurations.

But then we have some integers (in our current C/C++ implementation - 32 bit values). And when I use the MongoDB console to modify those integer values, Mongo always stores them back as Number (which is doulble in the C/C++ implementation). We will change the app to take double values where it expects integers but I was wondering if there is a way to force Mongo to store integers from its JavaScript console.

Any suggestions?

like image 702
Георги Кременлиев Avatar asked Jul 01 '12 11:07

Георги Кременлиев


People also ask

How do I change the datatype of a field in MongoDB?

You can change the data type of a field by using the data type selectors on the right of the field in the Atlas Cloud Cluster as well as MongoDB Compass . If you want to update it using Mongo shell or any specific drivers of MongoDB then you can refer to the $convert operator.

What is Int32 in MongoDB?

Int32. If a number can be converted to a 32-bit integer, mongosh will store it as Int32 . If not, mongosh defaults to storing the number as a Double . Numerical values that are stored as Int32 in mongosh would have been stored by default as Double in the mongo shell.

What is NumberLong in MongoDB?

NumberLong. The mongo shell treats all numbers as floating-point values by default. The mongo shell provides the NumberLong() wrapper to handle 64-bit integers. The NumberLong() wrapper accepts the long as a string: NumberLong("2090845886852")

How does MongoDB store float value?

Double: The double data type is used to store the floating-point values.


2 Answers

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.

like image 165
Stennie Avatar answered Oct 10 '22 17:10

Stennie


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.

like image 25
Nic Cottrell Avatar answered Oct 10 '22 16:10

Nic Cottrell