Reputation: 6916
I want to implement a minor file system like collection in MongoDB .
So say my object looks like this
{
"\":{
'autoexec.bat':{
name:'autoexec',
filetype:'bat',
size:1302
},
'users':{ /* its own tree */ },
'windows':{
'system':{
'autoexec.bat':{
name:'autoexec',
filetype:'bat',
size:1302123
}
}
}
}
I am wondering how to find the term 'autoexec.bat' in the most effiecient manner , Further for a file tree is there any better way to implement the same in Node.js or C++ ? I wish to implement features like search etc.
Upvotes: 1
Views: 143
Reputation: 42352
Last week at MongoNYC Kyle Banker gave a nice talk on schema design by example. I think your problem is extremely similar to his first example of musical genre hierarchy.
In effect, every file would have a document in the collection. It would have a parent field to identify its direct parent (directory it's in in your case) and an array of all its ancestors.
The queries that now become easy are - what directory is file "autoexec.bat" in - list all files in directory "foo" - list all files recursively in directory foo (all files with "foo" in its ancestors.
And don't forget, you also have an option of saving the full path name to a file/directory as well as its base name. This allows searching by leading parts of path if the field is indexed. It's also going to be unique, unlike just the file name.
As always, the key pieces of information are all the ways you'll need to query this collection and the performance and load expectations. Without that, it can be easy to pick a schema which will give you some challenges later on.
Upvotes: 0
Reputation: 230296
I'd say: don't do this in one gigantic document. Let each file be its own document in a collection, with references to parent and (probably) children. Almost all operations are now trivial. You only have to think about efficient way to read/delete the whole tree.
Upvotes: 2