How do I insert HTML into Mongodb?

前端 未结 3 395
情歌与酒
情歌与酒 2020-12-15 12:58

I keep getting kicked out of the shell when I try to paste in an HTML text file. How can this be done? Do I first need to use some javascript to encode it or something?

相关标签:
3条回答
  • 2020-12-15 13:24

    Use compass, you don't need to encode. Studio3T editor also works, but you need a license.

    0 讨论(0)
  • 2020-12-15 13:38

    rompetroll provided some good references to get started. I too found a good solution online which works fine:

    Link

    But I think there is still some confusion regarding "displaying html back to browser from db". So, following are the steps which removes this confusion :

    Step 1: Store html in mongodb

    Mongo store everything in form of object. Basically convert your html into utf-8 or ASCII complied string and then store it to mongo.

    <div>hi!</div> [from]
    
    &lt;div&gt;hi!&lt;/div&gt; [to UTF-8]
    

    Step 2: Display html to browser

    This is the step which is the source of confusion to some. People often forget to convert string back to html before displaying it to browser. You have to decode your html using the same method you used for encoding.

    &lt;div&gt;hi!&lt;/div&gt; [output from mongo (decode it to html)]
    
    <div>hi!</div> [browser output of above string]
    

    (browser will read it as string and display as it is and not html(hi!) and hence needs to be decoded)

    Link that I have provided have reference for both the steps.

    Hope this will help

    Cheers :)

    0 讨论(0)
  • 2020-12-15 13:40

    You need to remove or encode the control characters in your string.

    For example, paste your text in here, and encode to UTF-8 ECMAScript (which means javascript strings).

    ps: here an article about strings in javascript. tells you what needs escaping. http://docstore.mik.ua/orelly/webprog/jscript/ch03_02.htm

    0 讨论(0)
提交回复
热议问题