I was wondering which is better/faster:
- Having a separate collection of documents that just contain the image saved as binary data, and possibly some metadata.
- Or using GridFS to store the images.
JSON does not have the following data types that exist in BSON documents: data_binary, data_date, data_timestamp, data_regex,data_oid and data_ref. As a result using any tool that decodes BSON documents into JSON will suffer some loss of fidelity.
If maintaining type fidelity is important, consider writing a data import and export system that does not force BSON documents into JSON form as part of the process. The following list of types contain examples for how MongoDB will represent how BSON documents render in JSON.
data_binary
<bindata> is the base64 representation of a binary string. <t> is the hexadecimal representation of a single byte indicating the data type.
data_date
<date> is the JSON representation of a 64-bit signed integer for milliseconds since epoch.
data_timestamp
<t> is the JSON representation of a 32-bit unsigned integer for milliseconds since epoch. <i> is a 32-bit unsigned integer for the increment.
data_regex
<jRegex> is a string that may contain valid JSON characters and unescaped double quote (i.e. ") characters, but may not contain unescaped forward slash (i.e. /) characters. <jOptions> is a string that may contain only the characters g, i, m, and s.
data_oid
<id> is a 24 character hexadecimal string. These representations require that data_oid values have an associated field named “_id.”
data_ref
<name> is a string of valid JSON characters. <id> is a 24 character hexadecimal string.
See also
출처 - http://docs.mongodb.org/manual/core/import-export/#bson-json-type-conversion-fidelity
6 1 | |
9 | If your images are small you can store them as binary data in the documents in your collection. Just consider that you will be retrieving them every time you query your document (unless you exclude the 'image' field from your queries). However, if your images are larger I would use GridFS. GridFS has some features that make it very good at handling images that you should consider:
In terms of performance, reading/writing against a regular document should be no different than doing it against GridFS. I would not consider performance to be a differentiator in choosing either one. My personal recommendation is to go with GridFS, but you need to analyze for your particular use case. Hope this helps. |