Data Schema question

Hi all,

I’m starting an app build using Next.js and would like to use MongoDB to store user data but I have a ‘best practices’ question regarding the data schema. I understand that data that is accessed together should be stored together but I’m not sure how best to implement that given my use case.

I have two types of user. The first type “creators” can create and save assets (storing asset files on S3 and references in MongoDB), and the second type “consumers” can view all saved assets from all “creators”. Conversely, “consumers” can submit asset requests, so all “creators” should be able to see all asset requests from all “consumers”.

Being new to MongoDB, I’m not sure how best to split this out. Is there any efficiency issues with aggregating data from multiple collections and have a different user schema for each user type? I was considering having a generic User schema with a userType key, and two nested schemas for each user type but obviously this approach will mean latent objects for every single user which probably isn’t good practice. However I would like to aggregate data in as little db computes and blocking calls as possible which was another reason I was considering just one generic User schema for both user types.

I’d love to hear any pointers, or if anyone has built anything similar and how best to plan this out. It’ll be greatly appreciated.

1 Like

Hi,

I have two points to mention here:

MongoDB’s document model is ideal for scenarios where user types share some common fields but also require specific data. It allows you to avoid unnecessary joins and keeps data retrieval efficient.(which mean that you can use both “users” at the same collection)

Since MongoDB performs best when you model your schema around your queries, your schema should reflect how you’ll access and manipulate the data most frequently.

Finally, I recommend you to look for your usecases and see what looks better, store together or not. I can suggest you also to take a look in a MongoDB Data Modeling Path. Its a great learning path.

1 Like

Thankyou for trialing work.

Hi Jennysson,

Thank you for your response. Apologies for the late response, it took a while to be moderated and posted and I missed your message.

This is hugely helpful info, and the info I was hoping for. I’ve started the MongoDB university, and I’ll check the Modeling Path link you sent over too.

Many thanks again,

Tids

1 Like