Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot not able to update sharded collection on azure cosmos db(MongoDb)

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

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

If I try to update this document through any query tool using a command like -

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

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

  • 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" }

This is my DocumentObject:

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

This is my Repository Class -

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

and value i am trying to update

  • Value : doc : { "_id" : "12831221wadaee23", "dNumber" : "115", "processed": true }

the function I am trying to execute :

@Autowired
DocumentRepo docRepo;

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

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.

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.

like image 610
nispand mehta Avatar asked Mar 14 '19 18:03

nispand mehta


1 Answers

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);
            }
        };
    }
}

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

like image 83
przem Avatar answered Sep 21 '22 22:09

przem