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

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