在 MongodDB JSON-style doc 中存储 HTML 的好策略是什么

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15274251/
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-03 18:47:24  来源:igfitidea点击:

What is a good strategy to store HTML in MongodDB JSON-style doc

jsonmongodbmeteor

提问by Max Hodges

Is it OK to store HTML in MongoDB? Are there disadvantages? Are there better alternatives?

可以在 MongoDB 中存储 HTML 吗?有缺点吗?有更好的选择吗?

I'm a bit new to JSON, but I'm planning migrate my WordPress site to a custom meteor/mongodb site.

我对 JSON 有点陌生,但我计划将我的 WordPress 站点迁移到一个自定义的meteor/mongodb 站点。

In WordPress our "Posts" are essentially product records, and the "main content" is the product's description. These descriptions contain some HTML markup, such as "strong" tags, break tags, and href hyperlinks.

在WordPress中,我们的“帖子”本质上是产品记录,“主要内容”是产品的描述。这些描述包含一些 HTML 标记,例如“strong”标记、中断标记和 href 超链接。

<p>Who hasn't wished for a mini-Roomba to handle the arduous task of cleaning their iPhone screen? Now your dreams have come true! See the Takara web page for a <a href="http://www.takaratomy.co.jp/products/automee/" title="automee s" target="_blank">demo video.</a><strong>Colors: </strong> White, Red, Orange and Blue Runs on a single AA battery.<br> 1,575 yen</p>

Unlike XML, JSON lacks something like CDATA. Is it a bad idea to try and put HTML in my JSON-style doc description field? Are there special escape characters for doing this? Or should I store the HTML product description as a external, static file? Or are there other best practices?

与 XML 不同,JSON 缺少 CDATA 之类的东西。尝试将 HTML 放在我的 JSON 样式的文档描述字段中是一个坏主意吗?这样做是否有特殊的转义字符?还是应该将 HTML 产品描述存储为外部静态文件?或者还有其他最佳实践吗?

{
'_id':'236',
'name':'Tokyo Marui M9A1 Gas Blow Back Airsoft Gun',
'description':'<p>html here?</p>',
'tags': ['toys','outdoors']
...
}

Any tip, advice, links appreciated!

任何提示,建议,链接表示赞赏!

EDIT

编辑

Sample product description text added.

添加了示例产品描述文本。

EDIT2

编辑2

I found this stackoverflow article: How to store HTML data in MongoDB?

我找到了这篇 stackoverflow 文章: 如何在 MongoDB 中存储 HTML 数据?

and another on google https://groups.google.com/forum/?fromgroups=#!topic/mongodb-user/HW5XB5yox20

另一个在谷歌 https://groups.google.com/forum/?fromgroups=#!topic/mongodb-user/HW5XB5yox20

They seem to say it should be fine. But there isn't much discussion, so just seeking more confirmation.

他们好像说应该没问题。但是没有太多的讨论,所以只是寻求更多的确认。

Edit3

编辑3

additional reference https://groups.google.com/forum/?fromgroups=#!topic/mongodb-user/0m8KJ7mPWiQ

其他参考 https://groups.google.com/forum/?fromgroups=#!topic/mongodb-user/0m8KJ7mPWiQ

采纳答案by Max Hodges

OK I seem to have found enough articles to conclude:

好吧,我似乎找到了足够多的文章来总结:

It's perfectly fine to store html fragments and files in MongoDB as standard utf-8 encoded strings with a few caveats: http://docs.mongodb.org/manual/faq/developers/#when-should-i-use-gridfs

将 html 片段和文件作为标准 utf-8 编码字符串存储在 MongoDB 中非常好,但有一些注意事项:http: //docs.mongodb.org/manual/faq/developers/#when-should-i-use-gridfs