Recently I have been interviewing with a lot of firms for a Javascript Engineer role. Most of them have a similar process which involves an initial assignment followed by a couple or more technical interviews which can involve DS/Algo and stack specific questions. But there was this one firm that gave the best assignment and I thought why not share my experience with you all
I needed to design the database for a File explorer for storing images. Similar to the image shown below
Some of the operations that we should be able to perform are.
I was free to choose the database of my choice and I needed to provide the pseudo-code for all the queries mentioned above. Now let’s see its solution 😄
I am using MongoDB(NoSql) as the database of my choice. You can research more about it as to why Non-Relational databases are generally better for storing hierarchical data.
And as you can probably guess from the word hierarchical, there are multiple ways of storing hierarchical data in MongoDB and I am using something called storing ref to all ancestor folders. It is generally better if you are going to perform lots of operations in the nested files or folders (children). You can read more about it in Mongodb official docs
A Single collection called Block
is being used to represent both file and folder. It has a specific field called isFolder which is of Boolean
type and represents whether the current folder is a folder or a file. The Pseudocode for the Block
schema is given below
parentFolder
is a reference to current block’s parent. It means that the current block exist inside the parentFolderancestorFolders
is a list of all the ancestors in a sorted manner. The First element points to the root. So if folders exists in the order First>Second>Third
, Third’s ancestorFolders
will be like [First,Second]fileUrl
is a field to store the link pointing to its actual location on any bucket storage(AWS S3, Google cloud storage, etc.)name
The solution to mentioned Queries
Adding folder or file inside the root or any nested folder
Block
collection with the selected doc as its parent and use the newly created ancestor array in step 1 and 2 as its ancestor arrayStoring metadata like file size and format Already doing it(See collection code comments)
Delete files and folders with nested content
Search using file and folder name
We can easily search because we used text index on name
And there you have it. A clear and concise way of storing data for a file explorer inside MongoDB. Your way may change depending upon the exact use-case but this is one of the more balanced way of doing it. Make sure to comment if you have any doubts. I will see you in the next blog Till then, Happy coding 😁
Quick Links
Legal Stuff