2017-09-27 103 views
0

我正在尝试使用节点创建Azure文档数据库(波斯菊数据库)分区集合。在node.js中创建Azure DocumentDB分区集合

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

在这一点上我亏本,任何帮助将不胜感激。

由于

+0

的价值是什么,你要提供在'collection.azureOptions.offerThroughput'?它是否低于2500? –

+0

我从400开始,但是我在发布的文档中阅读过,它必须超过10,000。我提出了10,100,但我又有一个错误,说这个价值太高了。所以现在我使用10,000 –

回答

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

非常感谢你,我寻找了2天的文档,可以帮助我,但我专注于节点js文档...有时你需要去源代码。让我测试一下...... –

相关问题