如何在MongoDB Shell中将NumberDecimal转换为Double? [英] How to convert NumberDecimal to Double in MongoDB shell?

查看:1427
本文介绍了如何在MongoDB Shell中将NumberDecimal转换为Double?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文档,其"test"类型为NumberDecimal

I have a document with "test" filed as NumberDecimal type

{ "_id" : ObjectId("5d1a202e476381c30cd995a4"),  "test" : NumberDecimal("0.1") }

如何在mongodb shell中将测试"字段从NumberDecimal转换为Double?

How to convert "test" field from NumberDecimal to Double in mongodb shell?

我尝试执行

db.collection.find({"test": {$exists: true}}).forEach(function (x) {   x.test = parseFloat(x.test);   db.collection.save(x); });

但不能解决此问题,因为它返回NaN

but don't solve this problem because it return NaN

推荐答案

十进制类型不是JavaScript固有的,因此外壳中的NumberDecimal值是特殊的包装,表示存储在MongoDB中的BSON值.如果要使用parseFloat(),则可以将NumberDecimal转换为JSON,以便访问字符串值.例如,在您的原始代码中,这将是:parseFloat(x.test.toJSON()["$numberDecimal"]).

The decimal type is not native to JavaScript, so NumberDecimal values in the shell are special wrappers representing the BSON value stored in MongoDB. If you want to use parseFloat() you can convert a NumberDecimal to JSON in order to access the string value. For example, in your original code this would be: parseFloat(x.test.toJSON()["$numberDecimal"]) .

但是,更好的方法是使用聚合框架来操纵十进制值,包括算术运算(MongoDB 3.4+)和类型转换(MongoDB 4.0 +).

However, a better approach would be to use the aggregation framework to manipulate decimal values including arithmetic operations (MongoDB 3.4+) and type conversion (MongoDB 4.0+).

MongoDB 4.0+包含 $toDouble()表达式将数字值(十进制,整数,长整数,布尔值,日期,字符串)转换为双精度型. MongoDB 4.0中的聚合框架不能用于更新文档(除非您要使用

MongoDB 4.0+ includes a $toDouble() expression that will convert numeric values (decimal, int, long, boolean, date, string) to a double. The aggregation framework in MongoDB 4.0 cannot be used to update documents (unless you want to create a new collection or replace the existing collection using $out), so you would have to run an aggregation query to convert the values and then separately apply document updates:

// Find matching documents
var docs = db.collection.aggregate([
    { $match: {
        test: { $exists: true }
    }},

    // Add a new field converting the decimal to a double
    // (alternatively, the original "test" value could also be replaced)
    { $addFields: {
        testDouble: { $toDouble: "$test" }
    }}
])

// Update with the changes (Note: this could be a bulk update for efficiency)
docs.forEach(function (doc) {
     db.collection.update({ _id: doc._id}, {$set: { testDouble: doc.testDouble }});
});

// Check the results
> db.collection.find().limit(1)
{
    "_id" : ObjectId("5d1a202e476381c30cd995a4"),
    "test" : NumberDecimal("0.1"),
    "testDouble" : 0.1
}

MongoDB 4.2(当前在RC中)增加了对使用某些

MongoDB 4.2 (currently in RC) adds support for using some aggregation stages for updates, so in 4.2 the above update can be more concisely expressed as:

db.collection.updateMany(
    { test: { $exists: true }},
    [ { $addFields: { testDouble: { $toDouble: "$test" }}}]
)

这篇关于如何在MongoDB Shell中将NumberDecimal转换为Double?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆