如何将 mongodb 类型从 Double 或 Integer 更改为 String?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25954215/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:10:21  来源:igfitidea点击:

How to change a mongodb type from Double or Integer to String?

mongodb

提问by Alex Ryan

I used mongoimport to import some csv data into mongodb.

我使用 mongoimport 将一些 csv 数据导入到 mongodb 中。

It mostly created types correctly but there are a few instances where Doubles or Integers were created where Strings are desired.

它主要正确创建了类型,但也有一些实例在需要字符串的地方创建了双精度型或整数型。

I've tried several techniques to convert these fields to strings to no avail.

我尝试了多种技术将这些字段转换为字符串,但无济于事。

Here's what I have tried:

这是我尝试过的:

This produced an undesired change to an Object type (type=3):

这对对象类型(type=3)产生了不希望的变化:

db.temp.find( { 'name' : { $type : 16 } } ).forEach( function (x) { 
  x.name = new String(x.name); // convert field to string 
  db.temp.save(x); 
});

Result looked like this:

结果如下所示:

> db.temp.findOne({name: {$type:3}})
{
    "_id" : ObjectId("541a28ddbf8a2e3ee8439b58"),
    "name" : {
        "0" : "0",
        "1" : ".",
        "2" : "2",
        "3" : "2"
    }
}

This produced no change:

这没有产生任何变化:

db.temp.find({name: {$exists:true}}).forEach( function(x) {
  x.name = "" + x.name;
});

This produced no change:

这没有产生任何变化:

db.temp.find({name: {$exists:true}}).forEach( function(x) {
  x.name = x.name + "";
});

This produced no change:

这没有产生任何变化:

db.temp.find({name: {$exists:true}}).forEach( function(x) {
  x.name = "" + x.name + "";
});

This produced no change:

这没有产生任何变化:

db.temp.find({name: {$exists:true}}).forEach( function(x) {
  x.name = x.name.toString();
});

This produced an error: TypeError: Object 0.22 has no method 'toNumber'

这产生了一个错误:TypeError: Object 0.22 has no method 'toNumber'

db.temp.find({name: {$exists:true}}).forEach( function(x) {
  x.name = x.name.toNumber().toString();
});

回答by yaoxing

If you want to store the converted data, you'll need to updatethe document, otherwise the changed document goes to no where.

如果要存储转换后的数据,则需要update文档,否则更改的文档无处可去。

db.temp.find({name: {$exists:true}}).forEach( function(x) {
    db.temp.update({_id: x._id}, {$set: {name: x.name.toString()}});
});

As to the toNumberissue, it's not an build-in function. You may want to use parseIntor parseFloatinstead:

至于这个toNumber问题,它不是一个内置函数。您可能想要使用parseIntparseFloat代替:

parseInt("1000");  // output: 1000

回答by Jaymin

Convert Int to str:

将 Int 转换为 str:

db.dbname.find({fieldname: {$exists: true}}).forEach(function(obj) {
obj.fieldname= "" + obj.fieldname;
db.dbname.save(obj); });

I tried using this query. It was working for me. I think it is support till mongodb 3.4. In latest one might be now work.

我尝试使用此查询。它对我有用。我认为它是支持直到 mongodb 3.4。在最新的一个可能现在工作。