I have two schemas for products and folders
const ProductSchema = new Schema<TProductSchema>(
{
productName: String,
vendor: { type: Schema.Types.ObjectId, ref: 'Vendor' },
folder: { type: Schema.Types.ObjectId, ref: 'ProductFolder' },
}
);
const ProductFolderSchema = new Schema<TProductFolderSchema>(
{
folderName: { type: String, required: true },
parent: { type: Schema.Types.ObjectId, ref: 'ProductFolder' },
children: [{ type: Schema.Types.ObjectId, ref: 'ProductFolder' }],
}
);
I want the following functionality in my application:
When user selects a folder, all products whose folder
is equal to selected folder _id
and whose folder
is direct or indirect children of selected folder should be returned.
Folders and Products are separate entities: user can create and delete folders independently of products and vice versa.
The problem with current schema is that it's hard to implement finding products whose folder
is a child of selected folder.
I expect each folder to contain up to 100.000 products.
I was thinking if adding products
field that would contain product's _id
s to ProductFolder
is a good idea.
What schema design would you recommend? Should I add products
field or leave it as is and come up with a solution to find nested folders? Or maybe there's some other approach?