2

Currently I am new in reactive programming , I have added data in 2 documents, so currently what I am trying to do is to return only those data to client whose tokenIdentifier is same in both document.

Please refer the code below:

I have 2 collection that has

package com.mainApp;

import java.util.Date;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

@Document(value = "Token")
public class TokenData {

    @Id
    private String id;
    
    private String tokenIdentifier;
    
    private Date todayDate;
    
    
    public TokenData(String id, String tokenIdentifier, Date todayDate) {
        super();
        this.id = id;
        this.tokenIdentifier = tokenIdentifier;
        this.todayDate = todayDate;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getTokenIdentifier() {
        return tokenIdentifier;
    }

    public void setTokenIdentifier(String tokenIdentifier) {
        this.tokenIdentifier = tokenIdentifier;
    }

    public Date getTodayDate() {
        return todayDate;
    }

    public void setTodayDate(Date todayDate) {
        this.todayDate = todayDate;
    }

    
    

}
package com.mainApp;

import java.util.Date;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

@Document(value = "TURCollection")
public class TURCollection {
    @Id
    private String id;
    
    private String turIdentifier;
    
    private String tokenIdentifier;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getTurIdentifier() {
        return turIdentifier;
    }

    public void setTurIdentifier(String turIdentifier) {
        this.turIdentifier = turIdentifier;
    }

    public String getTokenIdentifier() {
        return tokenIdentifier;
    }

    public void setTokenIdentifier(String tokenIdentifier) {
        this.tokenIdentifier = tokenIdentifier;
    }
    
    

}

I have a controller which will return only those tokenData whose tokenData.getTokenIdentifier() == TURCollection.getTokenIdentifier().

So

@GetMapping(value = "/getAllToken")
    public Flux<TokenData> getToken(){
        /*List<TokenData> returnData = new ArrayList<TokenData>();
        List<TokenData> tokenData = tokenDataRepository.findAll().collectList().block();
        
        List<TURCollection> turCollection = turRepository.findAll().collectList().block();
        
        turCollection.forEach(tur -> {
            for(TokenData data : tokenData) {
                if(tur.getTokenIdentifier().equals(data.getTokenIdentifier())) {
                    returnData.add(data);
                }
            }
        });*/

but the block() code is not working in reactive programming Can anyone help how I can compare values of two flux in reactive way?

Toerktumlare
  • 12,548
  • 3
  • 35
  • 54

1 Answers1

2

You can use a combination of Flux.collectList and Mono.flatMap.

Mono<List<TokenData>> tokenData = tokenDataRepository.findAll().collectList();
Mono<List<TURCollection>> turCollection = turRepository.findAll().collectList();
Mono<List<TokenData>> result = turCollection.flatMap(turs ->
  Set<String> ids = turs.stream().map(TURCollection::getId).collect(toSet());
  return tokenData.map(tokens ->
    tokens.stream().filter(token -> ids.contains(token.getId())).collect(toList())
  );
);
Ilya Zinkovich
  • 4,082
  • 4
  • 25
  • 43
  • 1
    Thanks for the reply, but using this what is happening is that it is giving only those records whose getTokenIdentifier() is same at tht given position like if getTokenIdentifier() is same at 2nd index then only it give. My requirement is that let tokenDataRepository.findAll() has 3 records whose getTokenIdentifier() is xyz and turRepository.findAll() has 1 records whose getTokenIdentifier() is xyz. So in result I shud get 3 records. In short searching all those records from token document whose tokenidentifier is same as TURCOllection data. It shud return all the data of Token document. – Farahnaaz Pathan Jan 19 '21 at 03:23
  • @FarahnaazPathan check out the updated answer. – Ilya Zinkovich Jan 19 '21 at 12:09