如何设计mongodb集合

时间:2015-02-04 10:02:05

标签: mongodb collections

我的收藏场景是: 用户A可以阻止其他用户,其他用户可以阻止用户A,即多对多关系。 但我的问题是如何设计User类。

只需将所有被阻止的用户放入某种列表或将Block类嵌入到User类中。

可能的解决方案:

public class User
{

 String id;
  String username;
String password;
String email;
List<User> blockedUserEmails;
//getter and setters
}

或者

public class User
{

String id;
 String username;
String password;
String email;
//getter and setters

 List<String> blockedUserEmailsInAList;
 }

或者

  public class User
  {

String id;
String username;
String password;
 String email;

@Embedded
List<Blockee> blockedUserId;

//getter and setters
}

哪个班级最适合提议?

感谢任何帮助。

更新

用户A不喜欢用户B,C等将在同一组中并且可以阻止他们从组中。 也许用户B既不希望用户a是同一个组,也可以互相阻塞。当我显示可能的用户和组列表时,它们彼此不可见,例如用户A不能选择用户B或反之。

1 个答案:

答案 0 :(得分:1)

用户应该拥有他阻止的用户的ID列表,这样您就可以使用最少的提供信息识别被阻止的用户,并且您不必担心冗余信息不一致。

嵌入整个用户将导致递归爆炸,因为嵌入式阻止用户也可以拥有嵌入式用户,等等。

public class User {
  String id;
  String username;
  String password;
  String email;
  List<String> blockedUserIds;
  //getter and setters
}