2

Suppose I have to simple objects stored in mongoDB with many-to-many relationship:

public class Person {
  public Guid Id {get; set;}
  public string Name {get; set;}
  public List<Guid> Groups {get; set;}
}
public class Group {
  public Guid Id {get; set;}
  public string Title {get; set;}
  public List<Guid> Persons {get; set;}
}

So, any person might belong to a number of groups while any group consists of number of persons. I'm trying to find the most efficient way to retrieve persons (or groups) data using .NET driver and, if possible, avoid to run multiple queries for this purpose:

Processing "get person by Id" request I expect to have a data described as following:

PersonModel: {
   Id: <Guid>,
   Name: <string>,
   Groups: [
     { Id: <Guid>, Title: <string> },
     { Id: <Guid>, Title: <string> },
     ...
   ]
}

and the result of "get group by Id" request is expected to be as following:

GroupModel: {
   Id: <Guid>,
   Title: <string>,
   Persons: [
     { Id: <Guid>, Name: <string> },
     { Id: <Guid>, Name: <string> },
     ...
   ]
}

Any suggestions will be very appreciated. Thanks

  • Is there any restriction on "max no. of the person" in a group or "max. no. of the group" a person can join? – Jitendra Sep 16 '20 at 06:19
  • Is this help you a bit? https://stackoverflow.com/questions/2336700/mongodb-many-to-many-association. As far as I know using Spring with this @DBRef annotation to declare object for role like this private Set roles; – Stu_Dent Sep 16 '20 at 06:49
  • It can be modeled with persons embedded in group _or_ groups embedded in person (not both, I think). An important question is: which is the most queried (the person with groups _or_ the group with persons). For other queries you have to do with referencing, like using the aggregation's `$lookup`. Modeling N:N relationship is complex, and you have to analyze well ahead what kind of queries and updates are important and then build your data design. – prasad_ Sep 16 '20 at 08:35
  • In the worse case there is no restrictions on "max no. of the person" in a group or "max. no. of the group" – Timur Ginesin Sep 16 '20 at 08:37

0 Answers0