2017-09-27 43 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文檔...有時你需要去源代碼。讓我測試一下...... –

相關問題