使用Node.js通过ID从DocumentDB读取文档失败 [英] Read document from DocumentDB by ID using Node.js fails
问题描述
我正在尝试使用文档ID从DocumentDB读取单个文档。该集合有四个字段, author
是分区键。
I'm trying to read a single document from DocumentDB by using the document ID. The collection has four fields, author
being the partition key.
{
"id": string,
"author": string,
"title": string,
"year": int
}
我有两个函数用于读取存储在DocumentDB中的记录。 queryCollectionByBookId
按文档ID读取单个文档, queryCollectionGetBooks
返回集合中的所有文档。
I have two functions for reading the records stored into DocumentDB. queryCollectionByBookId
reads a single document by document id and queryCollectionGetBooks
returns all the documents in the collection.
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
返回以下错误消息。
queryCollectionGetBooks
function works fine, but queryCollectionByBookId
returns the error message below.
{错误:[提供的分区键在x-ms-partitionkey标头中,组件的数量少于集合中定义的组件。]}
是否有其他人看到此错误消息,发现了如何解决它?
Has anyone else seen this error message and found out how to resolve it?
推荐答案
这是对node.js客户端文档的疏忽,但你必须添加一个 options.partitionKey
属性到 readDocument(链接,选项,回调)
调用中的可选第二个参数。
It's an oversight in the documentation for the node.js client, but you have to add an options.partitionKey
property to the optional second parameter in your readDocument(link, options, callback)
call.
这也是文档的疏忽,但我认为partitionKey需要是一个包含单个元素的数组(例如 {options:{partitionKey:[myKey ]}}
[更新:我被告知它现在可以使用单个分区键值,但我自己没有确认。]
It's also an oversight in the docs but I think partitionKey needs to be an array with a single element (e.g. {options: {partitionKey: ["myKey"]}}
[UPDATE: I'm told that it will now work with a single partition key value but I haven't confirmed it myself.]
或者,您可以将 options.enableCrossPartitionQuery
属性设置为 true
,但效率较低。
Alternatively, you can set options.enableCrossPartitionQuery
property to true
but that is less efficient.
这篇关于使用Node.js通过ID从DocumentDB读取文档失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!