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

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.


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