[英]Read document from DocumentDB by ID using Node.js fails
I'm trying to read a single document from DocumentDB by using the document ID. 我正在尝试使用文档ID从DocumentDB中读取单个文档。 The collection has four fields,
author
being the partition key. 该集合有四个字段,
author
是分区键。
{
"id": string,
"author": string,
"title": string,
"year": int
}
I have two functions for reading the records stored into DocumentDB. 我有两个功能可以读取存储在DocumentDB中的记录。
queryCollectionByBookId
reads a single document by document id and queryCollectionGetBooks
returns all the documents in the collection. 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
function works fine, but queryCollectionByBookId
returns the error message below. queryCollectionGetBooks
函数可以正常工作,但是queryCollectionByBookId
返回以下错误消息。
{"Errors":["The partition key supplied in x-ms-partitionkey header has fewer components than defined in the the collection."]}
Has anyone else seen this error message and found out how to resolve it? 是否有其他人看到此错误消息并找到了解决方法?
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. 这是对node.js客户端文档的疏忽,但是您必须在
readDocument(link, options, callback)
调用中将options.partitionKey
属性添加到可选的第二个参数中。
It's also an oversight in the docs but I think partitionKey needs to be an array with a single element (eg {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.] 这也是文档中的一个疏漏,但我认为partitionKey必须是一个包含单个元素的数组(例如
{options: {partitionKey: ["myKey"]}}
[UPDATE:有人告诉我现在它可以使用单个元素分区键值,但我自己尚未确认。]
Alternatively, you can set options.enableCrossPartitionQuery
property to true
but that is less efficient. 另外,您可以将
options.enableCrossPartitionQuery
属性设置为true
但这效率较低。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.