无法使用数字分区键查询 Cosmos DB

Unable to query Cosmos DB using numeric partition key

我正在尝试使用数字字段作为分区键,但我无法 运行 在其上存储过程。我不确定我是否做错了什么或者不可能。

我有两个具有两个不同分区键的集合。

集合 1 中的示例文档

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

集合 2 中的示例文档

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

不同之处在于第二个集合中的组字段没有用双引号引起来。

这两个集合都将组作为分区键,我正在尝试执行 Azure Portal for Cosmos DB 中提供的示例存储过程。

虽然我能够 运行 第一次收集成功,但第二次收集产生错误 "no docs found"

我正在从 sql 服务器数据库导入数据,并且导入的 int 字段不带双引号。我搜索了很多但找不到与不带双引号的数字分区键相关的文档。

是否可以从数字字段中创建分区键??感谢您提供任何帮助,因为该字段最适合我的分区集合,并且非常有用。

请不要担心cosmos db存储过程中支持的数字分区键。您所做的一切 rightly.You 都遇到了意想不到的结果,因为 Azure 门户将分区键输入识别为 String 类型,即使您填充了 Number 类型。

您可以使用 cosmos db sdk 或 rest api 对其进行测试。例如,java sdk 示例如下:

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

    }
}

输出: