删除"\ n"从值字段中导致蒙戈数据库 [英] remove "\n" from values fields results in mongo db

查看:72
本文介绍了删除"\ n"从值字段中导致蒙戈数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从mongo db的搜索值结果中删除"\ n"而不从mongo db的值中删除它? 请我帮忙!

How can I remove "\n" from results of searching values in mongo db without remove it from the values of mongo db ? please I need your help!

推荐答案

包含2个文档的示例:

db.testcol.insert({
    textstr: "Hello World\nBye World\nHello World\n"
})
db.testcol.insert({
    textstr: "Testing\n123\nTesting\n"
})

db.testcol.find({}, {_id: 0, textstr: 1}).forEach(function(e, i) {
    e.textstr=e.textstr.replace(new RegExp('\n', 'g'), "");
    printjson(e);
});

将输出:

{
    "textstr" : "Hello WorldBye WorldHello World"
}
{
    "textstr" : "Testing123Testing"
}

值得一提的是,如果您想用另一个字符(例如空格)替换/n,则可以通过将replace的第二个参数更改为您选择的字符串来实现.

Worth noting that if you want to replace the /n with another character, such as a space, you can do that by changing the second parameter of replace to a string of your choice.

这篇关于删除"\ n"从值字段中导致蒙戈数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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