在node.js中创建Azure DocumentDB分区集合

在node.js中创建Azure DocumentDB分区集合,node.js,azure,azure-cosmosdb,Node.js,Azure,Azure Cosmosdb,我正在尝试使用node创建Azure Document DB cosmos DB分区集合 function _getOrCreateCollectionAsync(databaseUrl, collection){ var collectionUrl = `${databaseUrl}/colls/${collection.name}`, def = Q.defer(); console.log("getting collection: " + collectionUrl);

我正在尝试使用node创建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)' }
从上面可以看到,我有不同的选项来定义分区键,它们都返回相同的结果

我还尝试将其添加到reqOptions中,方法是使用此示例作为c中的指南,这表明分区键需要是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)' }
在这一点上,我感到不知所措,任何帮助都将不胜感激

感谢

如上所述,必须将分区键指定为对象并放入请求正文中

因此,请按以下方式更改您的代码:

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

您在collection.azureOptions.offerThroughput中提供的值是多少?是不是少于2500?我从400开始,但我在我发布的文档中看到,它必须超过10000。我输入了10100,但另一个错误是该值太高。所以现在我正在使用10000谢谢你这么多,我花了2天的时间寻找有帮助的文档,但我把重点放在节点js文档上。。。有时候你只需要找到源头。让我测试一下。。。