在执行collection.count(queryParams);
时,收到此错误“命令中的查询必须针对服务器上的单个分片键”。在Java中,如何从所有分片/多个分片中读取所有数据?我们正在使用Microsoft Azure Cosmos DB。
[ERROR] Caused by: com.mongodb.MongoCommandException: Command failed with error 61: 'query in command must target a single shard key' on server ".
样例代码:
MongoCollection<Document> collection = database.getCollection(eventsCollectionName);
Bson queryParams = Filters.and(
Filters.eq("mysystem.name", dmsEvent.getSystem().getName()),
Filters.eq("mysystem.environment", dmsEvent.getSystem().getEnvironment()),
Filters.eq("mymessage.type", dmsEvent.getMessage().getType()),
Filters.eq("myuser.syscode_id", dmsEvent.getUser_id().getSyscode_id()),
Filters.eq("myuser.condition_id", dmsEvent.getUser_id().getCondition_id()));
//This line is erroring out
long recordCount = collection.count(queryParams);
Cosmos DB收集文档样本:
分片键是“ / partitionKey”。
{
"_id" : ObjectId("5ab85424a43e6b11916ff6c3"),
"myuser" : {
"syscode_id" : 1,
"condition_id" : 1
},
"mysystem" : {
"name" : "4DL",
"environment" : "D"
},
"mymessage" : {
"type" : "A",
"occurance_count" : 1,
"rolltime" : "2018-03-25T18:00Z",
"timestamp" : "2018-03-25T18:00:11.150379Z"
},
"mydata" : {
"count" : "12",
"slot" : [
{
"length" : null,
"value" : "FF00"
}
]
},
"partitionKey" : "18",
"timeToLive" : 777600,
"_ts" : "2018-03-26 02:00:04.495"
}
最佳答案
这是固定的。 Count()
不起作用,已使用collection.find(queryParams)
并使用分片键执行collection.replaceOne(with the shard key)
关于java - 错误原因:com.mongodb.MongoCommandException:命令失败,错误61:命令中的查询必须针对服务器上的单个分片键,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49639871/