Storing Documents in a MongoDB Database?
Tags: mongodb, uncategorized, documentstorage, gridfs
Published: 2017-11-09 17:41:27

---
title: "Storing Documents in a MongoDB Database?"
published: true
tags: MongoDB,Uncategorized,document storage,GridFS
canonical_url: https://www.kenwalger.com/blog/uncategorized/storing-documents-mongodb-database/
cover_image: https://thepracticaldev.s3.amazonaws.com/i/jbt27f0zkza3l3lep1ot.png
---

[MongoDB](https://www.mongodb.com)&nbsp;is 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 “document can sometimes cause confusion. Is&nbsp;storing documents the job of the database?

For many of us, when we hear the word “document we think of a [Microsoft Word](https://www.amazon.com/gp/product/B0153V62P2/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=B0153V62P2&linkCode=as2&tag=kenwalgersite-20&linkId=76afa21bc5320f49aaf6bb8b88f9ecc7) ![](https://www.kenwalger.com/blog//ir-na.amazon-adsystem.com/e/ir?t=kenwalgersite-20&l=am2&o=1&a=B0153V62P2) document. Some may think of [Adobe Acrobat](https://www.amazon.com/gp/product/B072M1ZH28/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=B072M1ZH28&linkCode=as2&tag=kenwalgersite-20&linkId=29c1755619c848ed2313062bc3e132ac) ![](https://www.kenwalger.com/blog//ir-na.amazon-adsystem.com/e/ir?t=kenwalgersite-20&l=am2&o=1&a=B072M1ZH28)â€˜s Portable Document Format files. Or any other common “document concepts. In MongoDB and, in fact, many other database models the term&nbsp;[document](https://docs.mongodb.com/manual/core/document/) has a different meaning. I wrote a post about the [document model](https://www.kenwalger.com/blog/nosql/document-model/) 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.

What if we actually want to store “documents in a database? Is there a best practice for storing documents with MongoDB? What are some options to consider?

### MongoDB GridFS

[GridFS](https://docs.mongodb.com/manual/core/gridfs/) allows for the storage of more data in a single MongoDB document than is allowed by the [BSON specification](https://docs.mongodb.com/manual/reference/limits/#limit-bson-document-size) 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 [replication](https://docs.mongodb.com/manual/replication/), one could keep files deployed across multiple facilities and utilize MongoDB’s synchronization&nbsp;features. However, many file systems account for this as well on their own.

Storing documents with GridFS is a topic in an of itself and beyond to scope of this blog post. It’s definitely on the list of things to cover, though.

### Another Option for Storing Documents

Let me start here by saying that I am a bit biased in this regard. I have never been a huge fan of storing “documents”, images, videos, etc.&nbsp;_inside_ a database. It has always been my preference to store files on a file server, [network attached storage](https://www.amazon.com/gp/product/B016EWTC7E/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=B016EWTC7E&linkCode=as2&tag=kenwalgersite-20&linkId=76f40e61d4b2abb9bbe151589ed3030f) ![](https://www.kenwalger.com/blog//ir-na.amazon-adsystem.com/e/ir?t=kenwalgersite-20&l=am2&o=1&a=B016EWTC7E) system, or in the cloud somewhere like [Amazon S3](https://www.amazon.com/gp/product/B00YSILDE2/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=B00YSILDE2&linkCode=as2&tag=kenwalgersite-20&linkId=3e14030b7296b82d2c7079c397a9fdb8) ![](https://www.kenwalger.com/blog//ir-na.amazon-adsystem.com/e/ir?t=kenwalgersite-20&l=am2&o=1&a=B00YSILDE2).

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:

```
{
    _id : ,
    file_title : "<title>",
    file_URL : "<S3_URL>",
    uploaded_by : "<user_name>",
    created_at : <Timestamp>,
    updated_at : <Timestamp>,
    updated_by : "<user>",
    department : "<department_name>",
    tags : [
        { name : "<tag1>" },
        { name : "<tag2>" }
    ],
    version : <Timestamp>
}
```

This sort of design allows for some nice performance and query possibilities. If, for example, we want to search for “all documents tagged with the Python topic uploaded by Ken between 01 Jan 2016 and 01 Oct 2017 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.

### Wrap Up

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 _is_ 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.

Design your database schema to maintain the metadata and links for your documents and take advantage of MongoDB’s rich query language for finding your information.

* * *

Follow me on Twitter [@kenwalger](https://www.twitter.com/kenwalger) to get the latest updates on my postings.

There are a few MongoDB specific terms in this post. I created a [MongoDB Dictionary](https://www.echoskillstore.com/MongoDB-Dictionary/45103) skill for the [Amazon Echo](https://www.amazon.com/gp/product/B01DFKC2SO/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=B01DFKC2SO&linkCode=as2&tag=kenwalgersite-20&linkId=f9e513223de2525a72b95cf9561db55b)&nbsp;line of products. Check it out and you can say “Alexa, ask MongoDB for the definition of a&nbsp;document? and get a helpful response.

[![Facebook](https://i1.wp.com/www.kenwalger.com/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/facebook.png?resize=48%2C48&ssl=1 "Share on Facebook")](http://www.facebook.com/sharer.php?u=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Funcategorized%2Fstoring-documents-mongodb-database%2F&t=Storing%20Documents%20in%20a%20MongoDB%20Database%3F&s=100&p%5Burl%5D=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Funcategorized%2Fstoring-documents-mongodb-database%2F&p%5Bimages%5D%5B0%5D=https%3A%2F%2Fi1.wp.com%2Fwww.kenwalger.com%2Fblog%2Fwp-content%2Fuploads%2F2017%2F10%2Fwp_feature_store_documents-e1506995740114.png%3Ffit%3D125%252C125%26ssl%3D1&p%5Btitle%5D=Storing%20Documents%20in%20a%20MongoDB%20Database%3F "Share on Facebook")[![twitter](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&ssl=1 "Share on Twitter")](https://twitter.com/intent/tweet?url=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Funcategorized%2Fstoring-documents-mongodb-database%2F&text=Hey%20check%20this%20out "Share on Twitter")[![google_plus](https://i1.wp.com/www.kenwalger.com/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/google_plus.png?resize=48%2C48&ssl=1 "Share on Google+")](https://plus.google.com/share?url=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Funcategorized%2Fstoring-documents-mongodb-database%2F "Share on Google+")[![reddit](https://i1.wp.com/www.kenwalger.com/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/reddit.png?resize=48%2C48&ssl=1 "Share on Reddit")](http://www.reddit.com/submit?url=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Funcategorized%2Fstoring-documents-mongodb-database%2F&title=Storing%20Documents%20in%20a%20MongoDB%20Database%3F "Share on Reddit")[![linkedin](https://i2.wp.com/www.kenwalger.com/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/linkedin.png?resize=48%2C48&ssl=1 "Share on Linkedin")](http://www.linkedin.com/shareArticle?mini=true&url=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Funcategorized%2Fstoring-documents-mongodb-database%2F&title=Storing%20Documents%20in%20a%20MongoDB%20Database%3F "Share on Linkedin")[![mail](https://i2.wp.com/www.kenwalger.com/blog/wp-content/plugins/social-media-feather/synved-social/image/social/regular/96x96/mail.png?resize=48%2C48&ssl=1 "Share by email")](mailto:?subject=Storing%20Documents%20in%20a%20MongoDB%20Database%3F&body=Hey%20check%20this%20out:%20https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Funcategorized%2Fstoring-documents-mongodb-database%2F "Share by email")

The post [Storing Documents in a MongoDB Database?](https://www.kenwalger.com/blog/uncategorized/storing-documents-mongodb-database/) appeared first on [Blog of Ken W. Alger](https://www.kenwalger.com/blog).