C# MongoDB-使用多对多关系检索数据

C# MongoDB-使用多对多关系检索数据,c#,mongodb,many-to-many,C#,Mongodb,Many To Many,假设我必须使用多对多关系存储在mongoDB中的简单对象: 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;} pub

假设我必须使用多对多关系存储在mongoDB中的简单对象:

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;}
}
公共类人物{
公共Guid Id{get;set;}
公共字符串名称{get;set;}
公共列表组{get;set;}
}
公共课组{
公共Guid Id{get;set;}
公共字符串标题{get;set;}
公共列表人员{get;set;}
}
因此,任何人都可能属于多个群体,而任何群体都由多个人组成。我试图找到使用.NET驱动程序检索人员(或组)数据的最有效方法,如果可能,避免为此运行多个查询:

在处理“通过Id获取人员”请求时,我希望获得如下所述的数据:

PersonModel: {
   Id: <Guid>,
   Name: <string>,
   Groups: [
     { Id: <Guid>, Title: <string> },
     { Id: <Guid>, Title: <string> },
     ...
   ]
}
GroupModel: {
   Id: <Guid>,
   Title: <string>,
   Persons: [
     { Id: <Guid>, Name: <string> },
     { Id: <Guid>, Name: <string> },
     ...
   ]
}
PersonModel:{
Id:,
姓名:,
小组:[
{Id:,Title:},
{Id:,Title:},
...
]
}
“按Id获取组”请求的结果预计如下:

PersonModel: {
   Id: <Guid>,
   Name: <string>,
   Groups: [
     { Id: <Guid>, Title: <string> },
     { Id: <Guid>, Title: <string> },
     ...
   ]
}
GroupModel: {
   Id: <Guid>,
   Title: <string>,
   Persons: [
     { Id: <Guid>, Name: <string> },
     { Id: <Guid>, Name: <string> },
     ...
   ]
}
GroupModel:{
Id:,
标题:,
人员:[
{Id:,Name:},
{Id:,Name:},
...
]
}

如有任何建议,我们将不胜感激。谢谢

对一个人可以加入的“最多人数”或“最多人数”有什么限制吗?这对你有帮助吗。据我所知,使用带有@DBRef注释的Spring为角色声明对象,比如这个私有角色集;它可以用嵌入到组中的人或嵌入到人中的组来建模(我认为不是两者都是)。一个重要的问题是:哪一个是被询问最多的人(带组的人还是带组的人)。对于其他查询,您必须使用引用,例如使用聚合的
$lookup
。建模N:N关系非常复杂,您必须提前分析什么样的查询和更新是重要的,然后构建数据设计。在更糟糕的情况下,对组中的“最大人数”或“组中的最大人数”没有限制