java - 错误由 : com. mongodb.MongoCommandException 引起:命令失败,错误 61:命令中的查询必须针对服务器上的单个分片键

标签 java azure-cosmosdb azure-cosmosdb-mongoapi

在执行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/

相关文章:

java - 我不明白为什么 Hibernate 创建一个带有两个连接的查询

java - 泛型类中的泛型方法和非泛型类中的泛型方法的行为差异

c# - 使用 Azure DocumentDB 和 MVC5 处理动态数据

c# - 组合多个键以在 Cosmos db 中创建合成分区键

azure - 如何使用 Mongo API 在共享吞吐量 cosmosdb 中创建集合

Azure CosmosDB : Can One Set RBAC per Database/Collection

node.js - 无法连接到azure cosmos db中的Mongodb

java - 视频压缩不适用于 ffmpeg4android_lib 库

java - 为什么我们使用byte来读取二进制数据

azure - Cosmos DB 选择性区域复制