Passing collection's name as a parameter from the client?

Hello people,
I’m a beginner developer, creating a social media platform and I wanted to store posts as collections and comments as documents of each collection. Is it possible with mongoose to pass the collection’s name as a parameter from the client? <<function createCollection = (postTitle) => {
mongoose.model(postTitle, postSchema);}
Structure:
allPosts(database) => singlePost(collection) => comment(document)
Is there a better way to structure this?
Thank you!!:pray::pray::pray:

Hi :wave: @Amitay_Cohen,

Welcome to the MongoDB Community forums :sparkles:

I would rather suggest you two different approaches to design your schema for your social media platform: :point_down:

:point_right: Approach 1: Make two separate collections one for posts and another for comments and use $lookup to join both collections while fetching the data.

:point_right: Approach 2: Design a schema in which make comments a embedded document within the post document, as shown below:

{
_id : ObjectID("..."),
postId : 12234,
title: "Post_01",
comments: [
      {
        commentId: 23213
        userName: "Tom Cruise",
        comment: "Comment_01,
        dateofPosting: ISODate("2020-05-18T14:10:30.000Z")
        ...
      },
    ]
...
}

For example, in your application, a post can have about 10 to 20 comments, with each comment having a few of text. This will help think about storing all the comment data within each post itself. Then the schema design and the queries will be simple as all the related data is stored together.

The query will fetch post-related data like title, content, data, etc., and also all the comments associated with the post. You can use $projection to control the fields required on a particular page of the application. You can also, query for specific comments.

If you have any doubts, please feel free to reach out to us.

Best Regards,
Kushagra Kesav

Thank you very much @Kushagra_Kesav!
My application is supposed to store videos. A user can upload a video, and other users can reply to this video with their own videos. Similar to Twitter, but with videos instead of tweets, and with only 2 layers (you cant reply with a video to a replied video). I hope that makes sense.
I think that for such an application, it might be a good fit to store prime videos and replied videos in separate collections?
Thanks a lot!

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.