2

Getting this error "query in command must target a single shard key' on server " while doing collection.count(queryParams);. In Java, how can I read all the data from all the shard/multiple shards? We are using 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 ".

Sample Code:

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

Sample Cosmos DB Collection Document: Shard key is "/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"
}
Stennie
  • 63,885
  • 14
  • 149
  • 175
Dib
  • 59
  • 1
  • 5
  • To be specific here is my query: long recordCount = collection.count(queryParams); – Dib Apr 03 '18 at 22:12
  • 2
    add all your details by editin your question not into the comments – Muhammad Omer Aslam Apr 03 '18 at 22:34
  • As far as I can tell this error doesn't appear in the MongoDB server or Java driver source. Are you perhaps using CosmosDB? If not, what are your specific versions of MongoDB driver and server? What is the shard key for your collection and what are the contents of query params? A snippet of code would be helpful. – Stennie Apr 03 '18 at 22:59
  • Thanks for confirming you are using Cosmos DB. This is an issue specific to their MongoDB emulation; the `count()` would work with an actual sharded MongoDB deployment. Related post in the Azure forums: [support COUNT of a query on a partitioned collection](https://feedback.azure.com/forums/263030-azure-cosmos-db/suggestions/19676359-support-count-of-a-query-on-a-partitioned-collecti). – Stennie Apr 04 '18 at 02:10
  • Have added all the details in the question now. Thanks Stennie and everyone for your response. But how can I read this data if I want do so? Is this a known issue at Azure Cosmos DB end and there is no alternative of fix for this? – Dib Apr 04 '18 at 05:35
  • Hi , Can someone please tell me what will be the syntax/sample code in java targeted with shard key. "When using findAndModify in a sharded environment, the query must contain the shard key for all operations against the shard cluster." Sample code without Shard Key: MongoCollection collection; collection.find(queryParams); – Dib Apr 04 '18 at 23:32
  • This got fixed. Count() will not work , have used collection.find(queryParams) and collection.replaceOne(with the shard key) – Dib Apr 06 '18 at 20:51

1 Answers1

0

This got fixed. Count() will not work , have used collection.find(queryParams) and used the shard key to do collection.replaceOne(with the shard key)

Onkar Musale
  • 909
  • 10
  • 25
Dib
  • 59
  • 1
  • 5