[英]Read document from DocumentDB by ID using Node.js fails
我正在嘗試使用文檔ID從DocumentDB中讀取單個文檔。 該集合有四個字段, author
是分區鍵。
{
"id": string,
"author": string,
"title": string,
"year": int
}
我有兩個功能可以讀取存儲在DocumentDB中的記錄。 queryCollectionByBookId
通過文檔ID讀取單個文檔,而queryCollectionGetBooks
返回集合中的所有文檔。
var dbutils = {
queryCollectionByBookId: function(client, documentUrl) {
return new Promise((resolve, reject) => {
client.readDocument(documentUrl, function(err, doc) {
if (err) {
reject(err);
} else {
resolve(doc);
}
});
});
},
queryCollectionGetBooks: function(client, collectionUrl) {
return new Promise((resolve, reject) => {
client.readDocuments(collectionUrl).toArray((err, results) => {
if (err)
reject(err);
else {
resolve(results);
}
});
});
}
};
queryCollectionGetBooks
函數可以正常工作,但是queryCollectionByBookId
返回以下錯誤消息。
{"Errors":["The partition key supplied in x-ms-partitionkey header has fewer components than defined in the the collection."]}
是否有其他人看到此錯誤消息並找到了解決方法?
這是對node.js客戶端文檔的疏忽,但是您必須在readDocument(link, options, callback)
調用中將options.partitionKey
屬性添加到可選的第二個參數中。
這也是文檔中的一個疏漏,但我認為partitionKey必須是一個包含單個元素的數組(例如{options: {partitionKey: ["myKey"]}}
[UPDATE:有人告訴我現在它可以使用單個元素分區鍵值,但我自己尚未確認。]
另外,您可以將options.enableCrossPartitionQuery
屬性設置為true
但這效率較低。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.