【问题标题】:Creating a Azure DocumentDB Partitioned Collection in node.js在 node.js 中创建 Azure DocumentDB 分区集合
【发布时间】:2017-09-27 09:12:11
【问题描述】:

我正在尝试使用节点创建 Azure Document DB (cosmos DB) 分区集合。

function _getOrCreateCollectionAsync(databaseUrl, collection){
var collectionUrl = `${databaseUrl}/colls/${collection.name}`,
    def = Q.defer();

    console.log("getting collection: " + collectionUrl);

    client.readCollection(collectionUrl, (err, result) => {
        if (err) {
            if (err.code == 404) {
                console.log("collection " + collection.name + " not found... creating...");

                var colOptions = {};
                colOptions.offerThroughput = collection.azureOptions.offerThroughput || 10000; //default is 10k
                colOptions.partitionKey = ["/data"];
                // colOptions.partitionKey = ["data"];
                // colOptions.partitionKey = "/data";
                // colOptions.partitionKey = "data";

                var reqOptions = {
                    id : collection.name
                    //, partitionKey : ["/data"]
                }

                client.createCollection(databaseUrl, reqOptions, colOptions, (err, created) => {
                    if (err) {
                        console.log(err);
                        def.reject(err)
                    } else {
                        def.resolve(created);
                    }
                });
            } else {
                console.log(err);
                def.reject(err);
            }
        } else {
            def.resolve(result);
        }
    });

    return def.promise;
 }

(请忽略代码草率,我还在努力让它工作)

当我运行它时,我得到了这个错误

.......... working with collection: testCollection
getting collection: dbs/testDb/colls/testCollection
collection testCollection not found... creating...
{ code: 400,
  body: '{"code":"BadRequest","message":"Message: {\\"Errors\\":[\\"x-ms-documentdb-partitionkey header cannot be specified for this request\\"]}\\r\\nActivityId: (snip), Request URI: /apps/(snip)/services/(snip)/partitions/(snip)/replicas/(snip)"}',
  activityId: '(snip)' }

从上面可以看出,我有不同的选项来定义分区键,它们都返回相同的结果。

我还尝试使用此示例Create Collection in Azure DocumentDB with Different Partition Mode 作为指南(在 c# 中)将其添加到 reqOptions,这表明他的分区键需要成为 reqOptions 对象的一部分。当我这样做时,我得到了这个错误。

.......... working with collection: testCollection
getting collection: dbs/testDb/colls/testCollection
collection testCollection not found... creating...
{ code: 400,
  body: '{"code":"BadRequest","message":"The specified document collection is invalid.\\r\\nActivityId: (snip)"}',
  activityId: '(snip)' }

在这一点上我很茫然,任何帮助将不胜感激。

谢谢

【问题讨论】:

  • 您在collection.azureOptions.offerThroughput 中提供的价值是多少?是不是低于2500?
  • 我从 400 开始,但我在我发布的文档中看到,它必须超过 10,000。我输入了 10,100,但我收到另一个错误,说值太高。所以现在我使用 10,000

标签: node.js azure azure-cosmosdb


【解决方案1】:

正如Cosmos DB REST API documentation 所说,分区键必须指定为对象并放入请求正文中。

所以,请更改您的代码如下:

var colOptions = {};
colOptions.offerThroughput = collection.azureOptions.offerThroughput || 10000; //default is 10k

var reqOptions = {
    id: collection.name,
    partitionKey : { paths: ["/data"], kind: "Hash" }
}

client.createCollection(databaseUrl, reqOptions, colOptions, (err, created) => {
    if (err) {
        console.log(err);
        def.reject(err)
    } else {
        def.resolve(created);
    }
});

【讨论】:

  • 非常感谢,我找了 2 天的时间来寻找有用的文档,但我专注于 node js 文档……有时您只需要访问源代码即可。让我测试一下...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多