繁体   English   中英

使用响应式 Mongo 查询获取多个 Mono 对象

[英]Getting multiple Mono objects with reactive Mongo queries

我正在为 spring 启动使用 webflux 框架,我试图实现的行为是在数据库中创建一个新客户,如果它不存在(如果存在则抛出异常)并维护另一个国家代码数据库(如果新客户来自新国家,则添加到数据库中,如果国家已保存,则使用旧信息)

这是服务中的function:

    public Mono<Customer> createNewCustomer(Customer customer) {
    if(!customer.isValid()) {
        return Mono.error(new BadRequestException("Bad email or birthdate format"));
    }
    
    Mono<Customer> customerFromDB = customerDB.findByEmail(customer.getEmail());
    
    Mono<Country> countryFromDB = countryDB.findByCountryCode(customer.getCountryCode());

    Mono<Customer> c = customerFromDB.zipWith(countryFromDB).doOnSuccess(new Consumer<Tuple2<Customer, Country>>() {

        @Override
        public void accept(Tuple2<Customer, Country> t) {
            System.err.println("tuple " + t);
            if(t == null) {
                countryDB.save(new Country(customer.getCountryCode(), customer.getCountryName())).subscribe();
                customerDB.save(customer).subscribe();
                return;
            }
            Customer cus = t.getT1();
            Country country = t.getT2();
            if(cus != null) {
                throw new CustomerAlreadyExistsException();
            }
            if(country == null) {
                countryDB.save(new Country(customer.getCountryCode(), customer.getCountryName())).subscribe();
            }
            else {
                customer.setCountryName(country.getCountryName());
            }
        
            customerDB.save(customer).subscribe();
            
        }
    }).thenReturn(customer);
    return c;
}

我的问题是,如果找不到国家或客户,元组返回 null,而我需要单独了解它们是否存在,以便我可以正确保存到数据库中。 country customerFromDB.block() country == null永远不是真的

有没有做两个查询来获取它们的值?

用以下解决方案解决了它:

public Mono<Customer> createNewCustomer(Customer customer) {
    if(!customer.isValid()) {
        return Mono.error(new BadRequestException("Bad email or birthdate format"));
    }
    
     return customerDB.findByEmail(customer.getEmail())
             .defaultIfEmpty(new Customer("empty", "", "", "", "", ""))
             .flatMap(cu -> {
                if(!cu.getEmail().equals("empty")) {
                    return Mono.error(new CustomerAlreadyExistsException());
                }
                
                return countryDB.findByCountryCode(customer.getCountryCode())
                        .defaultIfEmpty(new Country(customer.getCountryCode(), customer.getCountryName()))
                        .flatMap(country -> {
                            customer.setCountryName(country.getCountryName());
                            customerDB.save(customer).subscribe();
                            countryDB.save(country).subscribe();
                            return Mono.just(customer);});
                
            });
}

我没有同时进行两个查询,而是查询一个结果,然后查询下一个结果,我认为这是一种被动的方式,但我愿意更正。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM