Which is better in MongoDB? Storing a list of members as a array in an object or member group relation collection

Viewed 100

I am building a social app with MongoDB. I have a scenario where I need to store the list of users in a group and their relation to the group. What is the optimized way to store it? I had come across 2 paths.

  • Saving the list of users inside the group collection

    {
      "groupId":"",
      "admins":[
        "1234","2345","3422"
      ],
      "moderators":[
        "3242"
      ],
      "members":[
        "2354","9865","6747"
      ]
    }
    
  • saving group id, user id, relation as a list in a separate collection in traditional style

    {
      "groupId":"1234",
      "userId":"8764",
      "relation":"Admin"
    },{
      "groupId":"0987",
      "userId":"4345",
      "relation":"Member"
    },{
      "groupId":"0987",
      "userId":"3456",
      "relation":"Admin"
    },{
      "groupId":"0987",
      "userId":"7654",
      "relation":"Member"
    },{
      "groupId":"0987",
      "userId":"8765",
      "relation":"Member"
    }
    

    My major Queries would be to

  • list the members of the group with their relation

  • search the members by their starting word

  • list the groups a user is a member of

Which approach will be best suited for the above quires.

** excuse me for the json formatting is not accepting in the editor code style.

0 Answers
Related