NoSQL schema for folder structure

mongodb, nosql

Solution

The docs give a great summary of the more popular/common ways to store hierarchical data in mongodb.

Embedding documents - have significant drawbacks

- Hard to search

- Hard to get back partial results

- Can get unwieldy if you need a huge tree. Further there is a limit on the size of documents in MongoDB – 16MB in v1.8 (limit may rise in future versions).

As you need to be able to retrieve single items - that is not likely to be the best option for your use case.

Array of ancestors or materialized path are likely to be much more suitable for what you've described - you could choose to use the full filepath for _id since that is unique and the path you would want to find data by more commonly.

Problem

I have documents that represent a folder structure. A folder can contain other folders (nested), theoretically unlimited levels deep but more realistically 3 or 4 levels for our application. I need to be able to retrieve a single item (a node) and perhaps embedding will make this task a bit difficult? Any suggestions?

Original source