admin管理员组文章数量:1405904
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
2 Answers
Reset to default 3Yes, 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.
本文标签: javascriptMongoDB and Nodejs saving int32 as doubleStack Overflow
版权声明:本文标题:javascript - MongoDB and Node.js: saving int32 as double - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744939113a2633373.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论