I'm trying to store a directory structure, including files and their content, in MongoDB. The work is part of a synching app, and is using in Node/Mongoose.

Now, I'm new to Mongo, and it's late here - is the idiomatic implementation as easy as it looks - ie, something like this?

var FileStoreSchema = new Schema({
  fullPath: {
    type: String,
    index: { unique: true }
  filename: String,
  [Metadata and other useful fields]  
  • 1
    That seems about right, but I'm pretty new to Mongo and the whole NoSQL craze myself...
    – yannis
    Mar 1, 2012 at 3:01

1 Answer 1


It depends largely on what operations you want to be able to efficiently support. Your current approach will make it easy to list all files within a directory and all descendants of a directory.

On the other hand moving files from one directory to another is more painful because there is no atomic modifier that allows you to modify a substring of a field. Instead, you'll have to pull down all of the file entries, modify them, then push them back as updates. (Someone correct me if I'm wrong about this)

With this structure it is also relatively difficult to list out the directory structure for traversal. The only way to know every folder is to load every file path and then parse them to see if they contain a new folder.

Does any of this matter? Maybe not, depends completely on what you are trying to do.

As another possible method, do you really want to represent one hierarchy? Or do you get a better representation have many smaller hierarchies? If the second, then you may get better results storing each hierarchy as a complete document, representing folder structure with the nesting of documents or via parent-child references.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.