【发布时间】:2019-07-12 02:13:24
【问题描述】:
我正在尝试从 mongodb 集合中获取所有文档,这些文档在过去 5 分钟内仅使用某些字段(例如 field1、field2、field3 等)进行了修改。如何编写 LiteralExpression 来获取特定字段(投影)?
我当前的文字表达式返回包含所有字段的文档(_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 管道的格式获取某些字段(投影)。
【问题讨论】:
标签: java spring-boot spring-data spring-integration spring-data-mongodb