Mongoose Schema for hierarchical data like a folder > subfolder > file -
how create hierarchical schema file system
var folder = new schema({ 'name' : string, 'isfile' : number, 'children' : [folder] });
can thing ???
the schema you've used embeds children folders inside parents. work, has couple of problems.
the first size of each folder document pretty big, depending on size of file system, , might run issues hitting size limits.
the other issue makes difficult directly find documents aren't @ top level.
a better solution store parent/child relationship references.
var folder = new schema({ name: string, isfile: boolean, parent: { type: schema.types.objectid, ref: 'folder' }, children: [{ type: schema.types.objectid, ref: 'folder' }] });
the ref property indicates in collection/model mongoose should looking referenced document, can find if query it. in case reference parent, folder, , list of chilren, documents of type folder.
storing references parent allow traverse tree, references children allow generate tree top down.
you can use populate
functionality of mongoose pull in details references. see the mongoose documentation more information on population , references.
n.b. changed isfile
boolean
field because assume trying store yes/no value?
Comments
Post a Comment