【发布时间】: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