我的以下 mongodb 查询按预期工作
db.importedDataItems.aggregate({
$match: {
mobile: "1234567890"
}
}, {
$group: {
_id: 'mobile',
calls: { $sum: '$calls' }
}
})
Run Code Online (Sandbox Code Playgroud)
但即使在参考这些 问题和教程之后,其等效的 Java 代码...
Aggregation agg = Aggregation.newAggregation(Aggregation.match(Criteria.where("mobile").is("1234567890"),
Aggregation.group("mobile").sum("calls").as("totalCalls"),
Aggregation.project("totalCalls"));
AggregationResults<Doc> results = mongoTemplate.aggregate(agg, "yoCollection",
Doc.class);
Doc doc = results.getMappedResults().get(0);
Run Code Online (Sandbox Code Playgroud)
...返回一个空列表并抛出IndexOutOfBoundsException虽然我的查询在控制台上返回结果!
我正在遵循Spring doc创建批处理服务。它实现了ItemWriter使用JdbcBatchItemWriter,所以您能帮我使用以下代码编写等效于MongoDb的代码MongoItemWriter吗?我发现有两个 使用MongoDb的教程,但是它们使用XML文件定义bean并显得过时。
@Configuration
@EnableBatchProcessing
public class BatchConfiguration {
// tag::readerwriterprocessor[]
@Bean
public ItemWriter<Person> writer(DataSource dataSource) {
JdbcBatchItemWriter<Person> writer = new JdbcBatchItemWriter<Person>();
writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<Person>());
writer.setSql("INSERT INTO people (first_name, last_name) VALUES (:firstName, :lastName)");
writer.setDataSource(dataSource);
return writer;
}
// end::readerwriterprocessor[]
@Bean
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
}
Run Code Online (Sandbox Code Playgroud)