小编par*_*rth的帖子

MongoTemplate:匹配特定条件的文档的键值总和

我的以下 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虽然我的查询在控制台上返回结果!

java mongodb spring-mongo mongotemplate spring-mongodb

5
推荐指数
1
解决办法
3186
查看次数

相当于Spring Batch中的作家的MongoDb吗?

我正在遵循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)

java spring mongodb spring-batch spring-data-mongodb

3
推荐指数
1
解决办法
3596
查看次数