使用反应式java,仅当flux为空时插入元素

使用反应式java,仅当flux为空时插入元素,java,project-reactor,reactive,Java,Project Reactor,Reactive,我有一个db,我想在这里看到我的条目和唯一的密钥。但有时我需要允许该键重复,所以我不能使用索引 我有类似的东西 Flux<Student> getStudentsBySomething(String something) {..} Mono<Void> insert(Student student) {...} Mono<Void> insertUnique(Student student) { // I want to write something

我有一个db,我想在这里看到我的条目和唯一的密钥。但有时我需要允许该键重复,所以我不能使用索引

我有类似的东西

Flux<Student> getStudentsBySomething(String something) {..}

Mono<Void> insert(Student student) {...}

Mono<Void> insertUnique(Student student) {
  // I want to write something like
  if (getStudentsBySomething().isEmpty()) {
     insert(student);
  } else{
     throw new RuntimeException(e);
  }
}
Mono insertUnique(学生){
getStudentsBySomething()
.LIST()
.flatMap(学生->{
if(students.isEmpty()){
返回插入(学生);
}否则{
抛出新的运行时异常(e);
}
};
}
Mono insertUnique(学生){
getStudentsBySomething()
.LIST()
.flatMap(学生->{
if(students.isEmpty()){
返回插入(学生);
}否则{
抛出新的运行时异常(e);
}
};
}

也许是这样的?那么你不必收集整个流量,如果有很多项目,这可能是不好的:

 Mono<Void> insertUnique(Student student) {
        return getStudentsBySomething("test")
                .hasElements()
                .flatMap(hasStudents -> {
                    if (!hasStudents) {
                        return insert(student);
                    } else {
                        throw new RuntimeException();
                    }
                });
    }
Mono insertUnique(学生){
返回getStudentsBySomething(“测试”)
.hasselements()
.flatMap(Hasstudens->{
如果(!hastudents){
返回插入(学生);
}否则{
抛出新的RuntimeException();
}
});
}

也许是这样的?那么你不必收集整个流量,如果有很多项目,这可能是不好的:

 Mono<Void> insertUnique(Student student) {
        return getStudentsBySomething("test")
                .hasElements()
                .flatMap(hasStudents -> {
                    if (!hasStudents) {
                        return insert(student);
                    } else {
                        throw new RuntimeException();
                    }
                });
    }
Mono insertUnique(学生){
返回getStudentsBySomething(“测试”)
.hasselements()
.flatMap(Hasstudens->{
如果(!hastudents){
返回插入(学生);
}否则{
抛出新的RuntimeException();
}
});
}

我喜欢
collectList
比我的版本有
count
更好!我喜欢
collectList
比我的版本有
count
更好!
 Mono<Void> insertUnique(Student student) {
        return getStudentsBySomething("test")
                .hasElements()
                .flatMap(hasStudents -> {
                    if (!hasStudents) {
                        return insert(student);
                    } else {
                        throw new RuntimeException();
                    }
                });
    }