最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

【规避解决】MongoDB中如何更新某字段的值为带换行的文字

MongoDB crifan 168浏览 0评论
折腾:
【未解决】小程序端如何显示换行符使得文字分段显示
期间,想要去更新MongoDB中某个值,是带换行的文字:
之前通过Mongo Compass去更新的,发现坑爹的是:表面上更新了,实际上把换行变成空格了。。
所以此处换用mongo shell试试。
换用mongo的命令行去试试能否保存换行的字符
What does the word
先用Mongo Compass中去查找出来:
{"stem_text": {"$regex": "What does the word*"}}
然后再去命令行中试试
➜  ~ mongo --host ip --port port -u user -p pwd --authenticationDatabase authDb
MongoDB shell version v3.6.3
connecting to: 
mongodb://ip:port/
MongoDB server version: 3.2.19
WARNING: shell and server versions do not match
Server has startup warnings:
2018-12-21T17:06:59.067+0800 I CONTROL  [initandlisten]
2018-12-21T17:06:59.067+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2018-12-21T17:06:59.067+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-12-21T17:06:59.067+0800 I CONTROL  [initandlisten]
2018-12-21T17:06:59.067+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-12-21T17:06:59.067+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2018-12-21T17:06:59.067+0800 I CONTROL  [initandlisten]
2018-12-21T17:06:59.068+0800 I CONTROL  [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 4096 processes, 65535 files. Number of processes should be at least 32767.5 : 0.5 times number of files.
2018-12-21T17:06:59.068+0800 I CONTROL  [initandlisten]
> show dbs
admin        0.000GB
dialog       0.272GB
evaluation   0.318GB
exam         0.000GB
gridfs      13.869GB
local        0.000GB
log          0.001GB
storybook    0.173GB
> use evaluation
switched to db evaluation
> show collections
audio.chunks
audio.files
evaluation
image.chunks
image.files
question
> db.question.findOne
db.question.findOne(            db.question.findOneAndDelete(   db.question.findOneAndReplace(  db.question.findOneAndUpdate(
> db.question.findOne({"_id": ObjectId("5c1777e1cc6df4563adf4bf6")})
{
    "_id" : ObjectId("5c1777e1cc6df4563adf4bf6"),
    "checkpoint" : [
        54,
        69,
        "句子理解"
    ],
    "stem_type" : "text",
    "difficulty" : 3.2,
    "stem_image" : "",
    "ave_answer_time" : 25,
    "audio_text" : "",
    "audio" : "",
    "question_number" : 424,
    "major_type" : "单选",
    "audio_length" : 0,
    "max_answer_time" : 50,
    "stem_text" : "What does the word \"visit\" mean in the sentence below?  At Thanksgiving, Tasha will visit her grandparents.",
    "sub_questions" : [
        {
            "correct_option" : [
                3
            ],
            "options" : [
                {
                    "option_index" : 1,
                    "option_text" : "Write a letter to",
                    "option_image" : ""
                },
                {
                    "option_index" : 2,
                    "option_text" : "Say thank you to",
                    "option_image" : ""
                },
                {
                    "option_index" : 3,
                    "option_text" : "Go and spend time with",
                    "option_image" : ""
                }
            ],
            "option_type" : "text",
            "question_texts" : [
                ""
            ]
        }
    ]
}
>
然后去试试:
db.evaluation.update({"_id": ObjectId("5c1777e1cc6df4563adf4bf6")}, {$set: {"stem_text": "What does the word \"visit\" mean in the sentence below? 
At Thanksgiving, Tasha will visit her grandparents."}})
其中below?后面,At Thanksgiving前面,是换行符
结果语法错误:
> db.evaluation.update({"_id": ObjectId("5c1777e1cc6df4563adf4bf6")}, {$set: {"stem_text": "What does the word \"visit\" mean in the sentence below?
2018-12-26T10:19:27.781+0800 E QUERY    [thread1] SyntaxError: unterminated string literal @(shell):1:89
> At Thanksgiving, Tasha will visit her grandparents."}})
mongodb update text contain new line
mongodb update text contain \n
mongodb   text include new line
javascript – Showing new lines <br> in the browser when having \n in the database – Stack Overflow
mongodb – Mongo query to remove “\n” from all _index fields? – Stack Overflow
OR Query mongodb from java with “like” and “line break” and “case insensitive” at the same time – Stack Overflow
感觉只有写代码去更新才可以?
document doesn’t break lines on new line characters in mongo shell – Google Groups
Textarea inserting \n in mongodb – help – Meteor forums
Solved: Insert a carriage return for a new line within the… – Alteryx Community
Storing Multi-Line Strings in JSON
意思是:JSON本身就不支持保存值的text的string是多行的string?
mongo json store multiple line
Multiline strings in JSON – Stack Overflow
javascript – How do I handle newlines in JSON? – Stack Overflow
Can a JSON value contain a multiline string – Stack Overflow
JSON
http://json.org
How to use large / multiline text as a field in mongodb | Andrey Mikhalchuk’s Blog
db.tests.insert( {
  name: 'test1',
  text: 'this is ' +
  'my superawesome ' +
  'multiline content'
} )
-》
db.tests.insert( {
  name: 'test2',
  text: "this is \nmy superawesome \nmultiline content"
} )
【总结】
所以目前的结论是:
JSON语法规范本身
就说了:
  • 不能包含换行符
    • 换行符属于:控制字符control character
    • JSON规范就不支持control character
    • JSON规范支持 \n(这是两个字符)
而MongoDB是基于JSON的,所以MongoDB本身不方便保存多行字符串,不是MongoDB的错,而是JSON的规范不支持(的不方便之处)。
想要MongoDB支持多行字符串,可以:
  • 方案1:把 单个的表示换行的控制字符 \n ,保存为 “\n”(2个字符,一个是反斜杠,一个是n)
    • 这样前端,比如web端,小程序段,移动端等,自己再去替换”\n”为\n
  • 方案2:把包含换行的字符串,保存为字符串列表,每个字符串是换行后的某一行
    • 比如:
"this is first line
and second line"
保存为:
[
  "this is first line"
  "and second line"
]
即可。

转载请注明:在路上 » 【规避解决】MongoDB中如何更新某字段的值为带换行的文字

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
18 queries in 0.077 seconds, using 9.34MB memory