0

我正在尝试将聚合操作的结果映射到 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 编辑 05/10/2021 它工作正常。我已经配置了一个自定义的,因为我想为这个聚合使用不同的池。

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

}

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

1 回答 1

0

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

现在不要使用 doNext()。一次接近它 1 步,以找出失败的确切位置

Flux<Document> result = reactiveMongoTemplate.aggregate(agg, BarProperty.class, Document.class);
于 2021-10-04T09:43:39.577 回答