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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With