non-existing 元素的 ReactiveMongoTemplate findOne 未被调用
ReactiveMongoTemplate findOne for non-existing element does not get called
我正在尝试从 collection 获取一份文档。文档可能不存在,如果 null
我想使用 return 默认值。
我的查询和转换:
return template.findOne(Query().addCriteria(Criteria.where("id")), DeviceSettings::class.java)
.map {
when (it) {
null -> {
defaultSettings(clock)
}
else -> {
listOf(
Instant.now(clock).toString(),
it.nextMeasurement.toString(),
it.shouldUpdateFirmware.toString()
)
}
}
}
}
不幸的是,上面的地图转换没有被调用。
当我简化对简单可调用对象的调用时,它被调用:
return Mono.fromCallable({
defaultSettings(clock)
})
Reactor 不在流中使用空值,并且 fineOne
应该 return 在没有结果时清空单声道。在你的情况下你应该使用
switchIfEmpty
运算符
template.findOne(...)
.map { listOf(...) }
.switchIfEmpty(Mono.just(defaultSettings(clock))
我正在尝试从 collection 获取一份文档。文档可能不存在,如果 null
我想使用 return 默认值。
我的查询和转换:
return template.findOne(Query().addCriteria(Criteria.where("id")), DeviceSettings::class.java)
.map {
when (it) {
null -> {
defaultSettings(clock)
}
else -> {
listOf(
Instant.now(clock).toString(),
it.nextMeasurement.toString(),
it.shouldUpdateFirmware.toString()
)
}
}
}
}
不幸的是,上面的地图转换没有被调用。
当我简化对简单可调用对象的调用时,它被调用:
return Mono.fromCallable({
defaultSettings(clock)
})
Reactor 不在流中使用空值,并且 fineOne
应该 return 在没有结果时清空单声道。在你的情况下你应该使用
switchIfEmpty
运算符
template.findOne(...)
.map { listOf(...) }
.switchIfEmpty(Mono.just(defaultSettings(clock))