春季WebFlux反应性Mongo散装操作(Java)

发布于 2025-01-31 05:45:26 字数 1076 浏览 3 评论 0 原文

https://github.com/spring-proing-proing-projects/spring-projects/spring-data-data-data-data--data--data--data--- mongodb/eskent/2821

我一直在寻找反应式泡沫操作,以将文档作为Spring WebFlux中的批量更新。

就像在Mongo模板中一样,

var bulkOps = mongoTemplate.bulkOps()
for(dto : List<DTO> DTOs) {
  Query query = new Query();
  query.addCriteria(Criteria.where(ID).is(dto.getId()));
  Update update = new Update()
                    .set(STATUS, dto.getStatus())
  bulkOps.updateOne(query, update)
}
bulkOps.execute();

有能力以反应性的方式实施该操作,因为ReactiveVemongotemplate看起来像当前不支持该操作?

so中的similiar主题: bute vishivemongotemplate bulk更新

https://github.com/spring-projects/spring-data-mongodb/issues/2821

https://jira.spring.io/browse/DATAMONGO-1922?redirect=false

I have been looking for ReactiveBulk operations to update documents as a batch in Spring WebFlux.

Like in the Mongo Template

var bulkOps = mongoTemplate.bulkOps()
for(dto : List<DTO> DTOs) {
  Query query = new Query();
  query.addCriteria(Criteria.where(ID).is(dto.getId()));
  Update update = new Update()
                    .set(STATUS, dto.getStatus())
  bulkOps.updateOne(query, update)
}
bulkOps.execute();

is there a workaround to implement that operation in reactive way since reactivemongotemplate look like does not support that operation currently?

similiar topic in so: Bulk Update with ReactiveMongoTemplate

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花开浅夏 2025-02-07 05:45:26

很快提醒大量与Updatemulti不同。

批量旨在在单个查询中写下多个对象,因此,更新各种对象。另一方面,UpdateMulti旨在更新所有线条,其中表达式匹配

为反应性散装,您应该能够使用 reactivemongotemplate 并实现类似的内容:

reactiveMongoTemplate.getCollection("collection_name")
    .flatMap(mongoCollection -> {
            List<UpdateOneModel<Document>> operations = DTOs.stream()
                .map(dto -> {
                    Document doc = new Document("status", dto.getStatus());
                    
                    reactiveMongoTemplate.getConverter().write(dto, doc);
                    
                    Document filter = new Document("id", dto.getId());
                    
                    return new UpdateOneModel<Document>(filter, new Document("$set", doc))
                }).toList();
        
            return Mono.from(mongoCollection.bulkWrite(operations));
    });

您还可以将自定义选项添加到 如果需要的话,bulkwrite()

如果需要更多过滤器,您可以将它们附加到文档

Document filter = new Document("id", dto.getId())
     .append("country", dto.getCountry);

Quickly remind that Bulk is different than UpdateMulti.

Bulk is meant to write multiple objects in a single query, therefore, update various objects. On the other side, UpdateMulti is intended to update all lines where expression matches

As for reactive bulk, you should be able to use ReactiveMongoTemplate and implement something like that:

reactiveMongoTemplate.getCollection("collection_name")
    .flatMap(mongoCollection -> {
            List<UpdateOneModel<Document>> operations = DTOs.stream()
                .map(dto -> {
                    Document doc = new Document("status", dto.getStatus());
                    
                    reactiveMongoTemplate.getConverter().write(dto, doc);
                    
                    Document filter = new Document("id", dto.getId());
                    
                    return new UpdateOneModel<Document>(filter, new Document("$set", doc))
                }).toList();
        
            return Mono.from(mongoCollection.bulkWrite(operations));
    });

You can also add custom options to bulkWrite() if you desire.

If more filter is needed, you can append them to the document

Document filter = new Document("id", dto.getId())
     .append("country", dto.getCountry);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文