mongodb的C#类设计问题

发布于 2024-12-05 20:51:44 字数 1012 浏览 0 评论 0原文

我在mongodb的C#类设计中遇到了一个问题,假设有两个类:

public class Group
{
    public Group()
    {
        Users = new List<User>();
    }
    public ObjectId Id { get; set; }
    public List<User> Users { get; set; }
}

public class User
{
    public ObjectId Id { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
}

并且在mongodb中有两个单独的集合对应于它们,架构是这样的:

Group:
{
    "_id" : ObjectId("4e7839d90a2248d934c182bd"),
    "Name" : "Group1",
    "Users" : [ 
        {"_id": ObjectId("4e7839d90a2248d934c154af"), "Name": "User1"}
      ]
}
User:
{
    "_id" : ObjectId("4e7839d90a2248d934c154af"),
    "Name" : "User1",
"Email": "[email protected]"
}

根据一些Mongodb设计原则,每次当我想显示每个用户的组中的名称,因此我将一些用户的字段嵌入组集合中,因此只需要一个查询,但是当插入组对象时,每个用户对象中的所有字段都将被插入,例如上面:电子邮件也将被插入插入,这会破坏初始模式并占用更多空间。那么,如何在插入组对象时剪切用户的电子邮件,你们有什么好主意吗?

I got a problem in C# class design for mongodb, suppose there two class:

public class Group
{
    public Group()
    {
        Users = new List<User>();
    }
    public ObjectId Id { get; set; }
    public List<User> Users { get; set; }
}

public class User
{
    public ObjectId Id { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
}

and there are two seperate collections corresponding to them in mongodb, the schema is like this:

Group:
{
    "_id" : ObjectId("4e7839d90a2248d934c182bd"),
    "Name" : "Group1",
    "Users" : [ 
        {"_id": ObjectId("4e7839d90a2248d934c154af"), "Name": "User1"}
      ]
}
User:
{
    "_id" : ObjectId("4e7839d90a2248d934c154af"),
    "Name" : "User1",
"Email": "[email protected]"
}

according to some Mongodb design principles, everytime when I want to display every users' name within a group, so I embed some user's fields in group collection, thus only single one query is needed, but when inserting a Group object, all fields in each User object will be inserted, for example above: the email also will be inserted, which destroy the initial schema and occupy more space. so how can I cut the User's email out when insert a Group object, do you guys have any good idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

日记撕了你也走了 2024-12-12 20:51:44

这是因为在对数据进行非规范化时,您需要为每个非规范化对象创建单独的类。对于 Group 中的 User 来说,您需要创建像这样的单独的类:

public class GroupUser
{
   public ObjectId Id {get;set;}

   public string Name {get;set;}
}

您的 Group 类将如下所示:

public class Group
{
  public Group()
  {
    Users = new List<GroupUser>();
  }
  public ObjectId Id { get; set; }
  public List<GroupUser> Users { get; set; }
}

然后 插入新的Group,您需要将数据从常规用户映射到非规范化的GroupUser。因此非规范化通常会导致大量映射和额外的类。但它使我们能够在从数据库加载数据时实现高性能。

This because when denormalizing data you need to create separate class for each denormilized object. In your case for User within Group you need to create separate class like this one:

public class GroupUser
{
   public ObjectId Id {get;set;}

   public string Name {get;set;}
}

Then your Group class will looks like this:

public class Group
{
  public Group()
  {
    Users = new List<GroupUser>();
  }
  public ObjectId Id { get; set; }
  public List<GroupUser> Users { get; set; }
}

And when you inserting new Group you need to map data from regular user to denormalized GroupUser. So denormalization usual lead to a lot of mapping and extra classes. But it allow us to achieve high performance in data loading from a database.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文