Spring Boot无法更新Azure Cosmos db(MongoDb)上的分片集合 [英] Spring Boot not able to update sharded collection on azure cosmos db(MongoDb)

查看:164
本文介绍了Spring Boot无法更新Azure Cosmos db(MongoDb)上的分片集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据库中存在一个集合"documentDev",其分片键为"dNumber" 样本文档:

I have a collection "documentDev" present in the database with sharding key as 'dNumber' Sample Document :

{
"_id" : "12831221wadaee23",
"dNumber" : "115",
"processed": false
}

如果我尝试使用-

db.documentDev.update({
  "_id" : ObjectId("12831221wadaee23"),
  "dNumber":"115"
},{
    $set:{"processed": true}}, 
{ multi: false, upsert: false}
)}`

它将正确更新文档. 但是如果我确实使用spring boot的mongorepository命令,例如 DocumentRepo.save(Object) 会引发异常

It updates the document properly. But if I do use spring boot's mongorepository command like DocumentRepo.save(Object) it throws an exception

  • 由以下原因引起:com.mongodb.MongoCommandException:命令失败,错误61:服务器上的查询by3prdddc01-docdb-3.documents.azure.com:10255'命令中的查询必须以单个分片键为目标.完整的响应为{"_t":"OKMongoResponse","ok":0,代码":61,"errmsg":命令中的查询必须以单个分片键为目标","$ err":命令中的查询必须以单个分片键为目标"}
  • Caused by: com.mongodb.MongoCommandException: Command failed with error 61: 'query in command must target a single shard key' on server by3prdddc01-docdb-3.documents.azure.com:10255. The full response is { "_t" : "OKMongoResponse", "ok" : 0, "code" : 61, "errmsg" : "query in command must target a single shard key", "$err" : "query in command must target a single shard key" }

这是我的DocumentObject:

This is my DocumentObject:

@Document(collection = "documentDev")
public class DocumentDev
{
@Id
private String id;
private String dNumber;
private String fileName;
private boolean processed;
}

这是我的存储库类-

@Repository
public interface DocumentRepo extends MongoRepository<DocumentDev, 
String> { }

和我正在尝试更新的值

  • 值:doc: { "_id":"12831221wadaee23", "dNumber":"115", 已处理":是 }
  • Value : doc : { "_id" : "12831221wadaee23", "dNumber" : "115", "processed": true }

我要执行的功能:

@Autowired
DocumentRepo docRepo;

docRepo.save(doc); // Fails to execute

注意:我在dNumber字段上启用了分片.而且我能够使用NoSQL Tool上的本机查询成功进行更新. 我还能够对非分片集合执行存储库保存操作.

Note: I have sharding enabled on dNumber field. And I am successfully able to update using Native queries on NoSQL Tool. I was also able to execute the Repository save operation on Non sharded collection.

更新:我可以通过使用MongoTemplate创建本机查询来更新文档-我的查询如下所示-

Update: I am able to update the document by creating native query using MongoTemplate - My Query looks like this -

public DocumentDev updateProcessedFlag(DocumentDev request) {
    Query query = new Query();
    query.addCriteria(Criteria.where("_id").is(request.getId()));
    query.addCriteria(Criteria.where("dNumber").is(request.getDNumber()));
    Update update = new Update();
    update.set("processed", request.isProcessed());
    mongoTemplate.updateFirst(query, update, request.getClass());
    return request;
}

但这不是通用的解决方案,因为任何其他字段可能都已更新,而我的文档也可能具有其他字段.

But this is not a generic solution as any other field might have update and my document may have other fields as well.

推荐答案

我遇到了相同的问题,并通过以下hack解决了该问题:

I had the same issue, solved with following hack:

@Configuration
public class ReactiveMongoConfig {

    @Bean
    public ReactiveMongoTemplate reactiveMongoTemplate(ReactiveMongoDatabaseFactory reactiveMongoDatabaseFactory,
            MongoConverter converter, MyService service) {
        return new ReactiveMongoTemplate(reactiveMongoDatabaseFactory, converter) {
            @Override
            protected Mono<UpdateResult> doUpdate(String collectionName, Query query, UpdateDefinition update,
                    Class<?> entityClass, boolean upsert, boolean multi) {
                query.addCriteria(new Criteria("shardKey").is(service.getShardKey()));
                return super.doUpdate(collectionName, query, update, entityClass, upsert, multi);
            }
        };
    }
}

最好有一个@ShardKey注释将文档字段标记为分片,并将其添加到自动查询中.

Would be nice to have an annotation @ShardKey to mark document field as shard and have it added to query automatically.

这篇关于Spring Boot无法更新Azure Cosmos db(MongoDb)上的分片集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆