Change MongoDB field type to integer from its console

We have an application that stores some configuration values ​​from C / C ++ to MongoDB and has the ability to restart (i.e., it starts for a while, someone interrupts the application, changes the configuration, then starts the application again, and it goes up there, where is it left). This works like a charm for logical and string configurations.

But then we have integers (in our current implementation of C / C ++ - 32-bit values). And when I use the MongoDB console to change these integer values, Mongo always saves them back as Number (which is doulble in the C / C ++ implementation). We will change the application to accept 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?

+5
source share
3 answers

In C / C ++, "meaning of the word", ints are not actually guaranteed to be 32-bit values. intmust be at least 16 bits, but generally consistent with the platform architecture (e.g. 32 or 64 bits).

@Jasd, JavaScript , (double C).

MongoDB NumberInt(..), 32- BSON NumberLong(..), 64- BSON.

+9

- :

db.Statistic.find({kodoId: {$exists: true}}).forEach(function (x) {
  x.kodoId = NumberInt(x.kodoId);
  db.Statistic.save(x);
});

, kodoId int . - :

db.Statistic.find({kodoId: {$exists: true}}, {kodoId: 1}).forEach(function (x) {
   db.Statistic.update({ _id: x._id },
      {$set: {
        kodoId: NumberInt(x.kodoId)
      }});
});

, kodoId (, ). , - .

+8

The MongoDB shell runs the JavaScript engine. And JavaScript has no type for integer. He only knows Numberwhich is the type of floating point numbers.
You can try using the MongoDB type NumberLong, but this did not work for me when I had the same problem.

0
source

All Articles