views:

20

answers:

1

I'm working on a project where I'm looking to store raw svg data inside my mongodb. Right now, it appears a bit goofy because I need to escape the svg string like so:

{ "_id" : ObjectId("4c61e60d4d02da615f175b6e"), "name" : "Triangle", "svg-data" : "<?xml version=\"1.0\" encoding=\"utf-8\"?> <!-- Generator: Adobe Illustrator 14.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 43363) --> <!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\"&gt; <svg version=\"1.1\" id=\"Layer_1\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" x=\"0px\" y=\"0px\" width=\"63.781px\" height=\"85.042px\" ... bunch of data elided here ... </svg>" }

This is somewhat acceptable but then I may need to de-escape (is that a word?) or decode on the client side once the json is passed back down from the server.

I'm curious if anyone else is doing this (I couldn't find any examples on google or stack overflow) and/or if there is any advice on the best way to do this (or a better way to do it).

A: 

Personally I haven't stored files in MongoDB yet, but according to mongodb.org it's quite efficient to store files in the database. Storing files in the database gives you the following advantages:

  • Because the files are chunked, you can fetch only part of a file. Useful for objects such as video, not so much for SVG.
  • All of your files are replicated together with your database, so you don't have to set up a separate replication system just for your files.

Now, the thing that seems to be holding you back is escaping the data. However, you only have to escape the SVG data if you manually type statements into the MongoDB shell. If you're using a driver to interact with the database, the driver will take care of escaping special characters in the data. When you read out the data in your code, the driver will also unescape the data for you.

An example to demonstrate, in the MongoDB shell:

var myObject = { "myKey": "Data with \"special\" characters." }
print(myObject.myKey)

In the first line I have to escape the quotes, because I manually type the statement. The MongoDB drivers will do this for you if you interact with the database from code.

The print() statement automatically unescapes the data, so the output will be:

Data with "special" characters.

Database drivers will also unescape strings automatically. If you send a JSON object to the client's browser and read out the values with JavaScript, the browser will also unescape the data.

When you manually prepare the data for insertion, you will have to escape special characters, there's no way around it. But as soon as you interact with the database from code, the database drivers will take care of this and you won't have to worry about it.

Niels van der Rest
Yeah, that makes total sense when using the driver v. the command line. I'll have to do some experiments to see how it works with mongoimport... we're using that as a data ingestion tool for now. Thanks for the detailed reply!
longda