【发布时间】:2018-04-03 22:11:26
【问题描述】:
在执行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"
}
【问题讨论】:
-
具体来说这里是我的查询:long recordCount = collection.count(queryParams);
-
通过编辑您的问题而不是 cmets 来添加您的所有详细信息
-
据我所知,此错误不会出现在 MongoDB 服务器或 Java 驱动程序源中。您是否正在使用 CosmosDB?如果没有,您的 MongoDB 驱动程序和服务器的特定版本是什么?您的集合的分片键是什么,查询参数的内容是什么?一个 sn-p 的代码会很有帮助。
-
感谢您确认您正在使用 Cosmos DB。这是他们的 MongoDB 仿真特有的问题;
count()将与实际的分片 MongoDB 部署一起使用。 Azure 论坛中的相关帖子:support COUNT of a query on a partitioned collection. -
现在已在问题中添加了所有详细信息。感谢 Stennie 和大家的回复。但是,如果我愿意,我该如何读取这些数据?这是 Azure Cosmos DB 端的一个已知问题吗?没有其他解决方法吗?
标签: java azure-cosmosdb azure-cosmosdb-mongoapi