Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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]  
});
share|improve this question
1  
That seems about right, but I'm pretty new to Mongo and the whole NoSQL craze myself... –  Yannis Rizos Mar 1 '12 at 3:01
add comment

1 Answer

up vote 1 down vote accepted

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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