ReactiveMongoTemplate 没有删除 return 数量的文档
ReactiveMongoTemplate does not return number of documents removed
我有这个实现来使用 ReactiveMongoTemplate 根据一些 id 删除文档。我正在尝试获取受影响文档列表的大小,但它始终 returns 0,并且由于它是被动的,我不确定如何获取已删除记录的数量
@Override
public int deleteMongoDataForGivenId(Long id) {
int deletedRecords = 0;
Query query = new Query();
query.addCriteria(where("id").is(id));
Flux<Object> deletedDocs = reactiveMongoTemplate.findAllAndRemove(query, Object.class, "SomeCollection");
if(!deletedDocs.collectList().block().isEmpty()) {
List<Object> listOfRecords = deletedDocs.collectList().block();
deletedRecords = listOfRecords.size();
}
}
反应性的方式是 return 一个 Mono<Long>
而不是阻塞并将单声道解压缩到 long
或 int
:
public Mono<Long> deleteMongoDataForGivenId(Long id) {
Query query = new Query();
query.addCriteria(where("id").is(id));
return reactiveMongoTemplate
.findAllAndRemove(query, MyDocument.class, "SomeCollection")
.count();
}
必须使用阻塞方法违背了反应式编程的目的,但如果您真的别无选择,您可以执行以下操作:
public Long deleteMongoDataForGivenId(Long id) {
Query query = new Query();
query.addCriteria(where("id").is(id));
return reactiveMongoTemplate
.findAllAndRemove(query, MyDocument.class, "SomeCollection")
.count()
// Please don't do this!!!
.share().block();
}
我有这个实现来使用 ReactiveMongoTemplate 根据一些 id 删除文档。我正在尝试获取受影响文档列表的大小,但它始终 returns 0,并且由于它是被动的,我不确定如何获取已删除记录的数量
@Override
public int deleteMongoDataForGivenId(Long id) {
int deletedRecords = 0;
Query query = new Query();
query.addCriteria(where("id").is(id));
Flux<Object> deletedDocs = reactiveMongoTemplate.findAllAndRemove(query, Object.class, "SomeCollection");
if(!deletedDocs.collectList().block().isEmpty()) {
List<Object> listOfRecords = deletedDocs.collectList().block();
deletedRecords = listOfRecords.size();
}
}
反应性的方式是 return 一个 Mono<Long>
而不是阻塞并将单声道解压缩到 long
或 int
:
public Mono<Long> deleteMongoDataForGivenId(Long id) {
Query query = new Query();
query.addCriteria(where("id").is(id));
return reactiveMongoTemplate
.findAllAndRemove(query, MyDocument.class, "SomeCollection")
.count();
}
必须使用阻塞方法违背了反应式编程的目的,但如果您真的别无选择,您可以执行以下操作:
public Long deleteMongoDataForGivenId(Long id) {
Query query = new Query();
query.addCriteria(where("id").is(id));
return reactiveMongoTemplate
.findAllAndRemove(query, MyDocument.class, "SomeCollection")
.count()
// Please don't do this!!!
.share().block();
}