50

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

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

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.

<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?

{
'_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

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

and another on google 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

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

1
  • Depending on size of files, and access patterns, it might be better to use static/loose HTML files as you'll avoid using the DB to fetch static data (and many web servers optimize for serving static files). Mar 7, 2013 at 18:03

2 Answers 2

25

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, noted in the MongoDB documentation "when should I use GridFS" (more on GridFS here)

GridFS is a specification for storing and retrieving files that exceed the BSON-document size limit of 16 MB.

ℹ️ NOTE GridFS does not support multi-document transactions.

Instead of storing a file in a single document, GridFS divides the file into parts, or chunks, and stores each chunk as a separate document.

When to use GridFS In MongoDB

In MongoDB, use GridFS for storing files larger than 16 MB. Do not use GridFS if you need to update the content of the entire file atomically.

2
  • 5
    new question: how? stackoverflow.com/questions/15275599/…
    – ChatGPT
    Mar 7, 2013 at 15:53
  • 3
    You could also consider using MongoDB's GridFS to store them (or use a second collection). You might not always want to pull all of the HTML out of a document, so you may want to split out the HTML. Just something to consider. It depends on your access patterns. Mar 7, 2013 at 18:02
1

You can store it as regular text type of String but validate the HTML well before saving to the database. Here is an example using express-validator and sanitize-html

body('description').not().isEmpty().trim().isLength({ min: 3 }).customSanitizer(value => {
    return sanitizeHtml(value, {
        exclusiveFilter: (frame) => {
            return frame.tag === 'script';
        },
        textFilter: (value) => {
            return value.replace(/\\n|\s\s/g, "").trim()
        }
    })
})

Here i'm getting the HTML from user in description req.body.description and as you can see express-validator to validate for specific rules and sanitize-html to control what i need alongside the default options and i'm stripping script tags as well as new line characters and spaces. I hope that helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.