最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Change the type of a MongoDB field to integer from its console - Stack Overflow

programmeradmin4浏览0评论

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?

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?

Share Improve this question edited Jul 2, 2012 at 11:06 Георги Кременлиев asked Jul 1, 2012 at 11:02 Георги КременлиевГеорги Кременлиев 5201 gold badge4 silver badges15 bronze badges 2
  • Resave the values as using parseFloat() or just don't put any speech marks around the value – Sammaye Commented Jul 1, 2012 at 11:06
  • 1 Actually try parseInt() sorry – Sammaye Commented Jul 1, 2012 at 11:12
Add a comment  | 

3 Answers 3

Reset to default 9

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.

发布评论

评论列表(0)

  1. 暂无评论