使用 Spring-Integration(仅限注释)获取具有特定字段(投影)的 mongodb 文档

Get mongodb documents with certain fields(projections) using Spring-Integration (annotations only)

我正在尝试从 mongodb 集合中获取所有文档,这些文档在过去 5 分钟内只修改了某些字段(比如字段 1、字段 2、字段 3 等等)。如何编写 LiteralExpression 来获取特定字段(投影)?

我当前的 Literal Expression return 个包含所有字段的文档(_id 是我集合中文档创建的时间戳):

public String getLiteralExpression(){
        long innerBoundary = Instant.now().minus(5, ChronoUnit.MINUTES).toEpochMilli();
        long outerBoundary = Instant.now().toEpochMilli();
        String expression = new StringBuilder()
                .append("{'_id': {'$gt': ")
                .append(innerBoundary)
                .append(", '$lt' : ")
                .append(outerBoundary)
                .append("}}")
                .toString();
        return expression;
    }
}

在 InboundChannelAdapter 中被调用为

@Bean
@InboundChannelAdapter(value = "pubSubChannel", poller = @Poller(fixedRate = "30000"))
public MessageSource<Object> DbReadingMessageSource() {

    Expression expression = new SpelExpressionParser().parseExpression("@myBean.getLiteralExpression()");

    MongoDbMessageSource messageSource = new MongoDbMessageSource(mongoTemplate, expression);
    messageSource.setCollectionNameExpression(new LiteralExpression(mongoTemplate.getCollectionName(MyEntity.class)));
    IntegrationFlows.from(messageSource);
    return messageSource;
}

有没有一种方法可以让我只使用 MongoTemplate 或 MongoDbFactory 而不是 LiteralExpression 以 MongoDbMessageSource 或任何其他可以馈送到我的 pubsubChannel 管道的格式获取某些字段(投影)。

事实上,expression 作为第二个 MongoDbMessageSource 参数可以解析为 org.springframework.data.mongodb.core.query.Query 对象。所以,它可能不仅仅是一个普通的 文字表达式 。对于您的投影用例,您可以这样写:

new BasicQuery([QUERY_STRING], [FIELD_STRING])

将从您的 @myBean.getLiteralExpression().

返回

Query API 非常灵活,并提供了许多流畅的挂钩,可以为最终的 MongoDB 查询进行配置。例如,它有一个 fields() for include/exclude 回调,用于您希望返回的特定字段。

Spring 数据 MongoDB 手册中有关 Query API 的更多信息:https://docs.spring.io/spring-data/mongodb/docs/2.1.5.RELEASE/reference/html/#mongodb-template-query

如果您想直接使用 MongoTemplate,您需要编写自定义代码,该代码应从具有相同 @InboundChannelAdapter 配置的 MethodInvokingMessageSource 包装器中调用。在该代码中,您仍然需要构建这样一个 Query 对象才能委托给 MongoTemplate.find()。这正是 MongoDbMessageSource.

中所做的

毫无疑问:您的 DbReadingMessageSource() 配置有点错误。您不能从该 bean 定义中调用 IntegrationFlows.from(messageSource);MongoDbMessageSource 必须配置为单独的 @Bean 并且已经没有 @InboundChannelAdapter 注释。 IntegrationFlow 必须是另一个 @Bean,你真的可以使用那个 from() 中的 DbReadingMessageSource()。但同样:没有 @InboundChannelAdapter。参见参考手册:https://docs.spring.io/spring-integration/docs/current/reference/html/#java-dsl-inbound-adapters