Spring Data MongoDB的输出类型聚合不映射自定义ReactiveMongoTemplate



我试图将聚合操作的结果映射到DTO。没有正确的映射,也没有错误或日志消息来指示出错的地方。

db.barProperty.aggregate([
{
$match: { domainObjectId : "044f4c4c-d481-4461-ae2f-301c78ff5c91"}
},
{
$sort: {
priority: 1,
issuedAt: -1
}
},
{
$group: {
"_id": "$field",
"value": {"$last": "$value"},
"issuedAt": {"$last": "$issuedAt"}
}
}
], { allowDiskUse: true });

输出:

/* 1 */
{
"_id" : "field1",
"value" : "2021-04-05T12:15:00Z",
"issuedAt" : ISODate("2021-06-14T00:10:00.000Z")
}
/* 2 */
{
"_id" : "field2",
"value" : "value2",
"issuedAt" : ISODate("2021-06-14T00:10:00.000Z")
}

…Java代码:

return reactiveMongoQueryTemplate.aggregate(Aggregation.newAggregation(BarProperty.class,
Aggregation.match(Criteria.where("domainObjectId").is(domainObjectId)),
Aggregation.sort(Direction.DESC, PRIORITY)
.and(Direction.ASC, ISSUED_AT),
Aggregation.group("field")
.last(VALUE).as(VALUE)
.last(ISSUED_AT).as(ISSUED_AT)
).withOptions(AggregationOptions.builder().allowDiskUse(true).build()), ProjectionProperty.class)
// TODO why is no ProjectionProperty being returned
.doOnNext(c -> {
System.out.println("found projection property");
})
.collectList();

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ProjectionProperty {
private String id;
private String value;
private OffsetDateTime issuedAt;
}

我已经尝试_id;And字段作为id字段的替代。

编辑05/10/2021使用OOTB reactiveMongoTemplate工作正常。我已经配置了一个自定义池,因为我想为这个聚合使用一个不同的池。

@ configuration类MultipleMongoConfig {

private final CustomMongoProperties customMongoProperties;
public MultipleMongoConfig(CustomMongoProperties customMongoProperties) {
this.customMongoProperties = customMongoProperties;
}
@Bean
public MongoClient reactiveMongoQueryClient() {
return MongoClients.create(createMongoClientSettings(customMongoProperties.getQuery()));
}
@Bean("mongoQueryTemplate")
public ReactiveMongoQueryTemplate reactiveMongoQueryTemplate() {
return new ReactiveMongoQueryTemplate(reactiveMongoQueryClient(), customMongoProperties.getQuery().getDatabase());
}
private MongoClientSettings createMongoClientSettings(MongoProperties mongoProperties) {
ConnectionString connectionString = new ConnectionString(mongoProperties.getUri());
return MongoClientSettings.builder()
.readConcern(ReadConcern.DEFAULT)
.writeConcern(WriteConcern.MAJORITY)
.readPreference(ReadPreference.primary())
.applyConnectionString(connectionString)
.build();
}

}

application.yaml

spring:
data:
mongodb:
database: @application.name@
authenticationDatabase: admin
uri: ${MONGO_URI}
query:
uri: ${MONGO_QUERY_URI}
database: ${MONGO_QUERY_DB}
auto-index-creation: true

请先试着弄清楚您的聚合是否有效,并返回一个文档,而不是试图映射到DTO。

现在不要使用doNext()。一步一步地接近它,找出它到底在哪里出错

Flux<Document> result = reactiveMongoTemplate.aggregate(agg, BarProperty.class, Document.class);

相关内容

  • 没有找到相关文章

最新更新