Mongo 节点驱动程序插入整数值而不是双精度值
Mongo Node Driver Inserts Integer Values Instead of Doubles
我正在使用 Mongo 本机 Java 脚本驱动程序将数据插入 Mongo 数据库。我读过的所有文档都说 Mongo 将其所有数值存储为双精度值,并且 Javascript 还将其所有数值存储为双精度值。我可以确认这在 shell:
中有效
PRIMARY> db.products.insertOne({id: 10, price: 10})
PRIMARY> db.products.insertOne({id: 11, price: 11.99})
PRIMARY> db.products.aggregate([{$project: { price: {$type: "$price"}}}])
{ "_id" : ObjectId("605525ed8f87f3c9bc136a69"), "price" : "double" }
{ "_id" : ObjectId("605525fb8f87f3c9bc136a6a"), "price" : "double" }
但是,当我从 Java 脚本驱动程序中插入值时,它们并不都是双精度数,而是整数形式的数字以 int
:[=14= 的 bsontype 存储]
-- Node
await products.insertOne({id: 1, price: 20});
await products.insertOne({id: 2, price: 20.85});
--Mongo Shell
PRIMARY> db.products.aggregate([{$project: { price: {$type: "$price"}}}])
{ "_id" : ObjectId("6055261ee2efe2622736a36e"), "price" : "int" }
{ "_id" : ObjectId("6055261ee2efe2622736a36f"), "price" : "double" }
在 mongo js 驱动程序中有没有办法强制它始终将数字存储为双精度数?我最终将其读入 Java 程序,该程序确实希望传入类型保持一致,因此如果我可以在插入时以这种方式制作它们,我想这样做。
选项 1
您可以使用https://plugins.mongoosejs.io/plugins/double
const Double = require('@mongoosejs/double');
const productsSchema = new Schema({ price: Double });
console.log(new Double(5));
Output - Double {
path: 5,
instance: 'Double',
validators: [],
getters: [],
setters: [],
options: SchemaTypeOptions {},
_index: null,
[Symbol(mongoose#schemaType)]: true }
你可以在这里查看源代码
https://github.com/mongoosejs/mongoose-double/blob/master/lib/index.js
选项 2
可以使用 mongoose.Decimal128
-
https://mongoosejs.com/docs/api.html#mongoose_Mongoose-Decimal128
const productsSchema = new Schema({ price: mongoose.Decimal128 });
Insert query will be like this -"price":{"$numberDecimal":"5"}
The Mongoose Decimal128 SchemaType. Used for declaring paths in your schema that should be 128-bit decimal floating points. Do not use this to create a new Decimal128 instance, use mongoose.Types.Decimal128 instead.
https://jira.mongodb.org/browse/SERVER-854
https://docs.mongodb.com/manual/core/shell-types/#numberint
https://docs.mongodb.com/manual/core/shell-types/#numberdecimal
The mongo shell treats all numbers as 64-bit floating-point double values by default. The mongo shell provides the NumberDecimal() constructor to explicitly specify 128-bit decimal-based floating-point values capable of emulating decimal rounding with exact precision. This functionality is intended for applications that handle monetary data, such as financial, tax, and scientific computations.
Mongo shell 数字的默认插入是 double 以插入 int 我们必须使用 NumberInt
进行类型转换
db.data.insertOne({ "numDouble":20.87, "numInt": NumberInt(1) })
https://github.com/Studio3T/robomongo/issues/622
另一个选项不确定它是否有效。
https://mongodb.github.io/node-mongodb-native/api-bson-generated/double.html
在插入值时将值类型转换为 Double
const Double = require("mongodb").Double;
await products.insertOne({id: 1, price: Double(20) });
console.log(new Double(5))
Output - Double { _bsontype: 'Double', value: 5 }
A class representation of the BSON Double type.
class Double()
Arguments:
value (number) – the number we want to represent as a double.
Returns: double
我正在使用 Mongo 本机 Java 脚本驱动程序将数据插入 Mongo 数据库。我读过的所有文档都说 Mongo 将其所有数值存储为双精度值,并且 Javascript 还将其所有数值存储为双精度值。我可以确认这在 shell:
中有效PRIMARY> db.products.insertOne({id: 10, price: 10})
PRIMARY> db.products.insertOne({id: 11, price: 11.99})
PRIMARY> db.products.aggregate([{$project: { price: {$type: "$price"}}}])
{ "_id" : ObjectId("605525ed8f87f3c9bc136a69"), "price" : "double" }
{ "_id" : ObjectId("605525fb8f87f3c9bc136a6a"), "price" : "double" }
但是,当我从 Java 脚本驱动程序中插入值时,它们并不都是双精度数,而是整数形式的数字以 int
:[=14= 的 bsontype 存储]
-- Node
await products.insertOne({id: 1, price: 20});
await products.insertOne({id: 2, price: 20.85});
--Mongo Shell
PRIMARY> db.products.aggregate([{$project: { price: {$type: "$price"}}}])
{ "_id" : ObjectId("6055261ee2efe2622736a36e"), "price" : "int" }
{ "_id" : ObjectId("6055261ee2efe2622736a36f"), "price" : "double" }
在 mongo js 驱动程序中有没有办法强制它始终将数字存储为双精度数?我最终将其读入 Java 程序,该程序确实希望传入类型保持一致,因此如果我可以在插入时以这种方式制作它们,我想这样做。
选项 1
您可以使用https://plugins.mongoosejs.io/plugins/double
const Double = require('@mongoosejs/double');
const productsSchema = new Schema({ price: Double });
console.log(new Double(5));
Output - Double {
path: 5,
instance: 'Double',
validators: [],
getters: [],
setters: [],
options: SchemaTypeOptions {},
_index: null,
[Symbol(mongoose#schemaType)]: true }
你可以在这里查看源代码
https://github.com/mongoosejs/mongoose-double/blob/master/lib/index.js
选项 2
可以使用 mongoose.Decimal128
-
https://mongoosejs.com/docs/api.html#mongoose_Mongoose-Decimal128
const productsSchema = new Schema({ price: mongoose.Decimal128 });
Insert query will be like this -"price":{"$numberDecimal":"5"}
The Mongoose Decimal128 SchemaType. Used for declaring paths in your schema that should be 128-bit decimal floating points. Do not use this to create a new Decimal128 instance, use mongoose.Types.Decimal128 instead.
https://jira.mongodb.org/browse/SERVER-854
https://docs.mongodb.com/manual/core/shell-types/#numberint
https://docs.mongodb.com/manual/core/shell-types/#numberdecimal
The mongo shell treats all numbers as 64-bit floating-point double values by default. The mongo shell provides the NumberDecimal() constructor to explicitly specify 128-bit decimal-based floating-point values capable of emulating decimal rounding with exact precision. This functionality is intended for applications that handle monetary data, such as financial, tax, and scientific computations.
Mongo shell 数字的默认插入是 double 以插入 int 我们必须使用 NumberInt
进行类型转换db.data.insertOne({ "numDouble":20.87, "numInt": NumberInt(1) })
https://github.com/Studio3T/robomongo/issues/622
另一个选项不确定它是否有效。
https://mongodb.github.io/node-mongodb-native/api-bson-generated/double.html
在插入值时将值类型转换为 Double
const Double = require("mongodb").Double;
await products.insertOne({id: 1, price: Double(20) });
console.log(new Double(5))
Output - Double { _bsontype: 'Double', value: 5 }
A class representation of the BSON Double type. class Double()
Arguments:
value (number) – the number we want to represent as a double. Returns: double