MongoDB: Many to many

Example: user's friends

Dec 1, 2021 | - views

@Document(collection = "users")
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class User {
  @Id
  private String id;

  private String name;

  @Version
  private Long version;

  @DocumentReference(lazy = true)
  private List<User> friends;
}
@Service
@RequiredArgsConstructor
public class UserService {
  private final UserRepository userRepository;

  public User create(String name) {
    User user = User.builder()
        .name(name)
        .build();

    userRepository.save(user);

    return user;
  }

  public void delete(String id) {
    userRepository.deleteById(id);
  }

  public void connect(String userId1, String userId2) {
    var user1 = userRepository.findById(userId1).orElseThrow();
    var user2 = userRepository.findById(userId2).orElseThrow();

    if (user1.getFriends() == null) {
      user1.setFriends(new ArrayList<>());
    }
    user1.getFriends().add(user2);

    if (user2.getFriends() == null) {
      user2.setFriends(new ArrayList<>());
    }
    user2.getFriends().add(user1);
    userRepository.saveAll(List.of(user1, user2)); // is not atomic
  }
}