Interested_Programmer
Interested_Programmer

Reputation: 322

Unable to query Cosmos DB using numeric partition key

I am trying to use a numeric field as a partition key but I am unable to run stored procedures on them. I am not sure if I am doing something wrong or if it is not possible.

I have two collections with two different partition keys.

A sample document from collection 1

{
 "id":"1",
 "group": "a"
}

A sample document from collection 2

{
 "id":"1",
 "group": 1
}

The difference is that the group field in second collection does not have double quotes around it.

Both the collections have group as the partition key and I am trying to execute the sample stored procedure provided in the Azure Portal for Cosmos DB.

While i am able to run the first collection successfully, the second collection produces an error of "no docs found".

second collection

first collection

I am importing the data from sql server database and the int fields are imported without double quotes. I searched quite a lot but could not find documentation relating to numeric partition key without double quotes.

Is it possible to do partition keys from numeric fields?? Any help appreciated as this field is the best fit for my collection for partitioning and would be really useful.

Upvotes: 2

Views: 999

Answers (1)

Jay Gong
Jay Gong

Reputation: 23782

Please don't worry about the numeric partition key which is supported in the cosmos db stored procedure definitely. You did everything rightly.You met the unexpected results because azure portal identify the partition key input as String type even though you filled Number type.

enter image description here

You could test it with cosmos db sdk or rest api. For example, java sdk sample as below:

import com.microsoft.azure.documentdb.*;

public class ExecuteSPTest {

    static private String YOUR_COSMOS_DB_ENDPOINT = "https://***.documents.azure.com:443/";
    static private String YOUR_COSMOS_DB_MASTER_KEY = "***";

    public static void main(String[] args) throws DocumentClientException {

        DocumentClient client = new DocumentClient(
                YOUR_COSMOS_DB_ENDPOINT,
                YOUR_COSMOS_DB_MASTER_KEY,
                new ConnectionPolicy(),
                ConsistencyLevel.Session);

        RequestOptions options = new RequestOptions();
        PartitionKey partitionKey = new PartitionKey(1);
        options.setPartitionKey(partitionKey);

        StoredProcedureResponse queryResults = client.executeStoredProcedure(
                "/dbs/db/colls/group/sprocs/sample",
                options,
                null);

        String document = queryResults.getResponseAsString();

        System.out.println(document);

    }
}

Output:

enter image description here

Upvotes: 2

Related Questions