HomeContact
MongoDB
Database design for a File Explorer
Ankit Brahmbhatt
March 01, 2021
2 min

Introduction

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

Problem

I needed to design the database for a File explorer for storing images. Similar to the image shown below

File Explorer demo

Some of the operations that we should be able to perform are.

  1. We need to allow the creation of a new folder or upload of a new file at the root of the storage or inside any subfolder.
  2. We should be able to store file metadata like creation date, file type, size, etc.
  3. We should be able to delete a folder along with all nested files and folders.
  4. We should be able to search by filename and folder name.

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 😄

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

Collections

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

Points to mention

  1. Field parentFolder is a reference to current block’s parent. It means that the current block exist inside the parentFolder
  2. Similarly ancestorFolders 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]
  3. fileUrl is a field to store the link pointing to its actual location on any bucket storage(AWS S3, Google cloud storage, etc.)
  4. To enable the search feature, a text index would be added to the field name

The solution to mentioned Queries

  • Adding folder or file inside the root or any nested folder

    1. We will need the id of the current directory so that we can find the doc which will become the parent
    2. Then we will get (copy ) the ancestor array from the selected doc and add the selected doc id to it
    3. We will create a new Document of Block collection with the selected doc as its parent and use the newly created ancestor array in step 1 and 2 as its ancestor array
  • Storing metadata like file size and format Already doing it(See collection code comments)

  • Delete files and folders with nested content

    1. Delete the selected block
    2. If the deleted block is a folder then delete all the other blocks which include the deleted block in their ancestorFolders list
  • Search using file and folder name

    We can easily search because we used text index on name

Conclusion

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 😁


Tags

javascriptmongodb
© 2021, All Rights Reserved.

Quick Links

Advertise with usContact Us

Social Media