javascript - MongoDB and Node.js: saving int32 as double - Stack Overflow

I have a variable defined in Node.js, for examplevar foo = 15I know JavaScript treat this value as a fl

I have a variable defined in Node.js, for example

var foo = 15

I know JavaScript treat this value as a floating number and I can't specify the type. I am using mongodb module to save this value into a document:

When I save the value with insert method and I pass the value to the method, I observe in my db document that the type is int32, but I need that mongodb save the value as double (15.0000).

It is possible to do so?

I have a variable defined in Node.js, for example

var foo = 15

I know JavaScript treat this value as a floating number and I can't specify the type. I am using mongodb module to save this value into a document:

When I save the value with insert method and I pass the value to the method, I observe in my db document that the type is int32, but I need that mongodb save the value as double (15.0000).

It is possible to do so?

Share Improve this question asked Dec 11, 2014 at 13:49 g3k0g3k0 1673 silver badges17 bronze badges 2
  • If strict typing is a requirement for your application then you should seriously reconsider your technology stack. – maerics Commented Dec 11, 2014 at 16:02
  • My example is simplified, the processes in place are more plex and the application is big (I need strict typing for this functionality and not for others). And I can't decide to change technology, I should find a solution in this situation. – g3k0 Commented Dec 12, 2014 at 8:59
Add a ment  | 

2 Answers 2

Reset to default 3

Yes, you can save it as double. But as indicated by @maerics type casting has to be done by you on Nodejs side. MongoDB won't do it for you.

For your solution:

var Double = require("mongodb").Double;
var foo = Double(15)

Now you can insert foo in a collection and you will see, it is there as double.

MongoDB likely only stores numerical values as doubles.

MongoDB uses BSON which is a binary-encoded serialization of JSON-like documents, which are in turn, a subset of JavaScript object literals. BSON does define "double" as well as "int32" and "int64" types; however, it is unclear if those integer types are usable directly by MongoDB drivers or if they are abstract types.

Looking at it another way; in JavaScript all numerical values are Numbers and Numbers are IEEE 754 double precision 64-bit floating point numbers. Because of this fact, JavaScript itself does not differentiate between integer and floating point types.

To further understand how MongoDB itself handles numerical types you can play with the mongo shell:

$ mongo foo
> db.foo.insert({looksLike:'int',   value:15})
> db.foo.insert({looksLike:'float', value:15.0})
> db.foo.insert({looksLike:'int64', value:Math.pow(2, 60)})
> db.foo.find({value:{ $type: 1 }}, {_id:0}) // Type 1 is "double".
{ "looksLike" : "int", "value" : 15 }
{ "looksLike" : "float", "value" : 15 }
{ "looksLike" : "int64", "value" : 1152921504606847000 } // Note inexact value.

As you can see, regardless of what numerical type one might expect to see, everything is stored as a double precision floating point value.

See also this discussion.

Ultimately, if you must use MongoDB and if some database clients must handle numerical values with specific numerical types (e.g. int32, int64) then the client itself must perform the type conversion because MongoDB will not do it for you.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744939113a4602211.html

相关推荐

  • javascript - MongoDB and Node.js: saving int32 as double - Stack Overflow

    I have a variable defined in Node.js, for examplevar foo = 15I know JavaScript treat this value as a fl

    1天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信