Search code examples
javamongodbspring-data-mongodb

How to use db references with reactive Spring Data MongoDB?


I'm new to MongoDB and Reactor and I'm trying to retrieve a User with its Profiles associated Here's the POJO :

public class User {

    private @Id String id;
    private String login;
    private String hashPassword;
    @Field("profiles") private List<String> profileObjectIds;
    @Transient private List<Profile> profiles; }

public class Profile {

    private @Id String id;
    private @Indexed(unique = true) String name;
    private List<String> roles; }

The problem is, how do I inject the profiles in the User POJO ?

I'm aware I can put a @DBRef and solve the problem but in it's documentation, MongoDB specify manual Ref should be preferred over DB ref.

I'm seeing two solutions :

  1. Fill the pojo when I get it :

    public Mono<User> getUser(String login) {
        return userRepository.findByLogin(login)
        .flatMap(user -> ??? );
    }
    

I should do something with profileRepository.findAllById() but I don't know or to concatene both Publishers given that profiles result depends on user result.

  1. Declare an AbstractMongoEventListener and override onAfterConvert method :

But here I am mistaken since the method end before the result is Published

public void onAfterConvert(AfterConvertEvent<User> event) {
    final User source = event.getSource();
    source.setProfiles(new ArrayList<>());
    profileRepository.findAllById(source.getProfileObjectIds())
    .doOnNext(e -> source.getProfiles().add(e))
    subscribe();
}

Solution

  • TL;DR

    There's no DBRef support in reactive Spring Data MongoDB and I'm not sure there will be.

    Explanation

    Spring Data projects are organized into Template API, Converter and Mapping Metadata components. The imperative (blocking) implementation of the Template API uses an imperative approach to fetch Documents and convert these into domain objects. MappingMongoConverter in particular handles all the conversion and DBRef resolution. This API works in a synchronous/imperative API and is used for both Template API implementations (imperative and the reactive one).

    Reuse of MappingMongoConverter was the logical decision while adding reactive support as we don't have a need to duplicate code. The only limitation is DBRef resolution that does not fit the reactive execution model.

    To support reactive DBRefs, the converter needs to be split up into several bits and the whole association handling requires an overhaul.

    Reference : https://jira.spring.io/browse/DATAMONGO-2146

    Recommendation

    Keep references as keys/Id's in your domain model and look up these as needed. zipWith and flatMap are the appropriate operators, depending on what you want to archive (enhance model with references, lookup references only).

    On a related note: Reactive Spring Data MongoDB comes partially with a reduced feature set. Contextual SpEL extension is a feature that is not supported as these components assume an imperative programming model and thus synchronous execution.