【问题标题】:Spring Data MongoDB outputType for aggregate not mapping with custom ReactiveMongoTemplate用于聚合的 Spring Data MongoDB outputType 未与自定义 ReactiveMongoTemplate 映射
【发布时间】:2021-10-04 08:10:56
【问题描述】:

我正在尝试将聚合操作的结果映射到 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;和字段代替 id 字段。

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

@配置 类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();
}

}

应用程序.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

【问题讨论】:

  • 如果排除到 POJO 的映射,您的 java 代码是否真的返回任何结果?以字符串形式返回 Java 中的聚合结果。
  • 输出类型不是可选的,我试过用 String.class 切换 ProjectionProperty,class 但它仍然没有打印 doOnNext
  • 您是否尝试将 mongo 日志降低到调试级别?当使用 Java 编写时,查询本身仍然是您所期望的吗? logging.level.org.springframework.data.mongodb.core.ReactiveMongoTemplate=DEBUG
  • @NicoVanBelle 我确实已经启用了调试日志记录,并且查询本身确实返回了我所期望的。

标签: java mongodb aggregation-framework spring-data-mongodb-reactive


【解决方案1】:

您能否先尝试确定您的聚合是否有效并返回文档而不是尝试映射到 DTO。

暂时不要使用 doNext()。一步一步地接近它,找出它到底在哪里失败

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

【讨论】:

    猜你喜欢
    • 2017-03-22
    • 1970-01-01
    • 2020-07-27
    • 2015-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多