{"id":608,"date":"2017-10-03T05:53:38","date_gmt":"2017-10-03T12:53:38","guid":{"rendered":"https:\/\/www.kenwalger.com\/blog\/?p=608"},"modified":"2017-12-01T07:58:32","modified_gmt":"2017-12-01T15:58:32","slug":"storing-documents-mongodb-database","status":"publish","type":"post","link":"https:\/\/www.kenwalger.com\/blog\/nosql\/mongodb\/storing-documents-mongodb-database\/","title":{"rendered":"Storing Documents in a MongoDB Database?"},"content":{"rendered":"<p><a href=\"https:\/\/www.mongodb.com\">MongoDB<\/a>\u00a0is considered a document database. As happens from time to time in the world of technology, the same word is used for different concepts. In this case, the word &#8220;document&#8221; can sometimes cause confusion. Is\u00a0storing documents the job of the database?<\/p>\n<p>For many of us, when we hear the word &#8220;document&#8221; we think of a <a href=\"https:\/\/www.amazon.com\/gp\/product\/B0153V62P2\/ref=as_li_tl?ie=UTF8&amp;camp=1789&amp;creative=9325&amp;creativeASIN=B0153V62P2&amp;linkCode=as2&amp;tag=kenwalgersite-20&amp;linkId=76afa21bc5320f49aaf6bb8b88f9ecc7\" target=\"_blank\" rel=\"noopener\">Microsoft Word<\/a><img loading=\"lazy\" decoding=\"async\" style=\"border: none !important; margin: 0px !important;\" src=\"\/\/ir-na.amazon-adsystem.com\/e\/ir?t=kenwalgersite-20&amp;l=am2&amp;o=1&amp;a=B0153V62P2\" alt=\"\" width=\"1\" height=\"1\" border=\"0\" \/> document. Some may think of <a href=\"https:\/\/www.amazon.com\/gp\/product\/B072M1ZH28\/ref=as_li_tl?ie=UTF8&amp;camp=1789&amp;creative=9325&amp;creativeASIN=B072M1ZH28&amp;linkCode=as2&amp;tag=kenwalgersite-20&amp;linkId=29c1755619c848ed2313062bc3e132ac\" target=\"_blank\" rel=\"noopener\">Adobe Acrobat<\/a><img loading=\"lazy\" decoding=\"async\" style=\"border: none !important; margin: 0px !important;\" src=\"\/\/ir-na.amazon-adsystem.com\/e\/ir?t=kenwalgersite-20&amp;l=am2&amp;o=1&amp;a=B072M1ZH28\" alt=\"\" width=\"1\" height=\"1\" border=\"0\" \/>&#8216;s Portable Document Format files. Or any other common &#8220;document&#8221; concepts. In MongoDB and, in fact, many other database models the term\u00a0<a href=\"https:\/\/docs.mongodb.com\/manual\/core\/document\/\">document<\/a> has a different meaning. I wrote a post about the <a href=\"https:\/\/www.kenwalger.com\/blog\/nosql\/document-model\/\">document model<\/a> as it pertains specifically to MongoDB and would recommend having a look for further information. In a nutshell, however, in MongoDB, a document is an individual record of information.<\/p>\n<p>What if we actually want to store &#8220;documents&#8221; in a database? Is there a best practice for storing documents with MongoDB? What are some options to consider?<\/p>\n<h3>MongoDB GridFS<\/h3>\n<p><a href=\"https:\/\/docs.mongodb.com\/manual\/core\/gridfs\/\">GridFS<\/a> allows for the storage of more data in a single MongoDB document than is allowed by the <a href=\"https:\/\/docs.mongodb.com\/manual\/reference\/limits\/#limit-bson-document-size\">BSON specification<\/a> of 16MB. This allows for the database record to be split into separate files on disk. This can allow for some pretty specific benefits in addition to those laid out in the GridFS documentation. For example, with <a href=\"https:\/\/docs.mongodb.com\/manual\/replication\/\">replication<\/a>, one could keep files deployed across multiple facilities and utilize MongoDB&#8217;s synchronization\u00a0features. However, many file systems account for this as well on their own.<\/p>\n<p>Storing documents with GridFS is a topic in an of itself and beyond to scope of this blog post. It&#8217;s definitely on the list of things to cover, though.<\/p>\n<h3>Another Option for Storing Documents<\/h3>\n<p>Let me start here by saying that I am a bit biased in this regard. I have never been a huge fan of storing &#8220;documents&#8221;, images, videos, etc.\u00a0<em>inside<\/em> a database. It has always been my preference to store files on a file server, <a href=\"https:\/\/www.amazon.com\/gp\/product\/B016EWTC7E\/ref=as_li_tl?ie=UTF8&amp;camp=1789&amp;creative=9325&amp;creativeASIN=B016EWTC7E&amp;linkCode=as2&amp;tag=kenwalgersite-20&amp;linkId=76f40e61d4b2abb9bbe151589ed3030f\" target=\"_blank\" rel=\"noopener\">network attached storage<\/a><img loading=\"lazy\" decoding=\"async\" style=\"border: none !important; margin: 0px !important;\" src=\"\/\/ir-na.amazon-adsystem.com\/e\/ir?t=kenwalgersite-20&amp;l=am2&amp;o=1&amp;a=B016EWTC7E\" alt=\"\" width=\"1\" height=\"1\" border=\"0\" \/> system, or in the cloud somewhere like <a href=\"https:\/\/www.amazon.com\/gp\/product\/B00YSILDE2\/ref=as_li_tl?ie=UTF8&amp;camp=1789&amp;creative=9325&amp;creativeASIN=B00YSILDE2&amp;linkCode=as2&amp;tag=kenwalgersite-20&amp;linkId=3e14030b7296b82d2c7079c397a9fdb8\" target=\"_blank\" rel=\"noopener\">Amazon S3<\/a><img loading=\"lazy\" decoding=\"async\" style=\"border: none !important; margin: 0px !important;\" src=\"\/\/ir-na.amazon-adsystem.com\/e\/ir?t=kenwalgersite-20&amp;l=am2&amp;o=1&amp;a=B00YSILDE2\" alt=\"\" width=\"1\" height=\"1\" border=\"0\" \/>.<\/p>\n<p>How then does one combine these two concepts? Store the files on disk outside the database, but utilize the power of the database as well? Well, you use the MongoDB document model and flexible schema to handle the metadata of the file, along with a link to the location of the file. Something along the lines of:<\/p>\n<pre>{\n    _id : ,\n    file_title : \"&lt;title&gt;\",\n    file_URL : \"&lt;S3_URL&gt;\",\n    uploaded_by : \"&lt;user_name&gt;\",\n    created_at : &lt;Timestamp&gt;,\n    updated_at : &lt;Timestamp&gt;,\n    updated_by : \"&lt;user&gt;\",\n    department : \"&lt;department_name&gt;\",\n    tags : [\n        { name : \"&lt;tag1&gt;\" },\n        { name : \"&lt;tag2&gt;\" }\n    ],\n    version : &lt;Timestamp&gt;\n}\n<\/pre>\n<p>This sort of design allows for some nice performance and query possibilities. If, for example, we want to search for &#8220;all documents tagged with the Python topic uploaded by Ken between 01 Jan 2016 and 01 Oct 2017&#8221; we can query for that. We also get to utilize the file system itself which is often better optimized for file storage reads and writes.<\/p>\n<h3>Wrap Up<\/h3>\n<p>In this short post, I hope to have clarified the concept of a document that is stored in a MongoDB database, like a Word or PDF file. Versus the concept of a document that <em>is<\/em> a MongoDB database record. While MongoDB can be utilized for file storage, in my opinion, file storage should be left to, well, file storage systems.<\/p>\n<p>Design your database schema to maintain the metadata and links for your documents and take advantage of MongoDB&#8217;s rich query language for finding your information.<\/p>\n<hr \/>\n<p>Follow me on Twitter <a href=\"https:\/\/www.twitter.com\/kenwalger\">@kenwalger<\/a> to get the latest updates on my postings.<\/p>\n<p>There are a few MongoDB specific terms in this post. I created a <a href=\"https:\/\/www.echoskillstore.com\/MongoDB-Dictionary\/45103\">MongoDB Dictionary<\/a> skill for the <a href=\"https:\/\/www.amazon.com\/gp\/product\/B01DFKC2SO\/ref=as_li_tl?ie=UTF8&amp;camp=1789&amp;creative=9325&amp;creativeASIN=B01DFKC2SO&amp;linkCode=as2&amp;tag=kenwalgersite-20&amp;linkId=f9e513223de2525a72b95cf9561db55b\" rel=\"noopener noreferrer\">Amazon Echo<\/a>\u00a0line of products. Check it out and you can say &#8220;Alexa, ask MongoDB for the definition of a\u00a0document?&#8221; and get a helpful response.<\/p>\n<a class=\"synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-facebook nolightbox\" data-provider=\"facebook\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Facebook\" href=\"https:\/\/www.facebook.com\/sharer.php?u=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F608&#038;t=Storing%20Documents%20in%20a%20MongoDB%20Database%3F&#038;s=100&#038;p&#091;url&#093;=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F608&#038;p&#091;images&#093;&#091;0&#093;=https%3A%2F%2Fi0.wp.com%2Fwww.kenwalger.com%2Fblog%2Fwp-content%2Fuploads%2F2017%2F10%2Fwp_feature_store_documents-e1506995740114.png%3Ffit%3D125%252C125%26ssl%3D1&#038;p&#091;title&#093;=Storing%20Documents%20in%20a%20MongoDB%20Database%3F\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"Facebook\" title=\"Share on Facebook\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/96x96\/facebook.png?resize=48%2C48&#038;ssl=1\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-twitter nolightbox\" data-provider=\"twitter\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Twitter\" href=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F608&#038;text=Hey%20check%20this%20out\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"twitter\" title=\"Share on Twitter\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/96x96\/twitter.png?resize=48%2C48&#038;ssl=1\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-reddit nolightbox\" data-provider=\"reddit\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Reddit\" href=\"https:\/\/www.reddit.com\/submit?url=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F608&#038;title=Storing%20Documents%20in%20a%20MongoDB%20Database%3F\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"reddit\" title=\"Share on Reddit\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/96x96\/reddit.png?resize=48%2C48&#038;ssl=1\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-linkedin nolightbox\" data-provider=\"linkedin\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Linkedin\" href=\"https:\/\/www.linkedin.com\/shareArticle?mini=true&#038;url=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F608&#038;title=Storing%20Documents%20in%20a%20MongoDB%20Database%3F\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"linkedin\" title=\"Share on Linkedin\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/96x96\/linkedin.png?resize=48%2C48&#038;ssl=1\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-mail nolightbox\" data-provider=\"mail\" rel=\"nofollow\" title=\"Share by email\" href=\"mailto:?subject=Storing%20Documents%20in%20a%20MongoDB%20Database%3F&#038;body=Hey%20check%20this%20out:%20https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F608\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"mail\" title=\"Share by email\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/96x96\/mail.png?resize=48%2C48&#038;ssl=1\" \/><\/a>","protected":false},"excerpt":{"rendered":"<p>MongoDB\u00a0is considered a document database. As happens from time to time in the world of technology, the same word is used for different concepts. In this case, the word &#8220;document&#8221; can sometimes cause confusion. Is\u00a0storing documents the job of the database? For many of us, when we hear the word &#8220;document&#8221; we think of a &hellip; <a href=\"https:\/\/www.kenwalger.com\/blog\/nosql\/mongodb\/storing-documents-mongodb-database\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Storing Documents in a MongoDB Database?&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":616,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pmpro_default_level":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[980,979],"yst_prominent_words":[982,104,983,298,286,538,767,984,972,975,971,87,411,976,722,575,945,973,968,969],"class_list":["post-608","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mongodb","tag-document-storage","tag-gridfs","pmpro-has-access"],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/uploads\/2017\/10\/wp_feature_store_documents-e1506995740114.png?fit=125%2C125&ssl=1","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8lx70-9O","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/posts\/608","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/comments?post=608"}],"version-history":[{"count":8,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/posts\/608\/revisions"}],"predecessor-version":[{"id":620,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/posts\/608\/revisions\/620"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/media\/616"}],"wp:attachment":[{"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/media?parent=608"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/categories?post=608"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/tags?post=608"},{"taxonomy":"yst_prominent_words","embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/yst_prominent_words?post=608"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}