使用 Node.js 按 ID 从 DocumentDB 读取文档失败
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
returns 读取集合中的所有文档。
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
returns 下面的错误信息。
{"Errors":["The partition key supplied in x-ms-partitionkey header has fewer components than defined in the the collection."]}
有没有其他人看到此错误消息并找到解决方法?
这是 node.js 客户端文档中的疏忽,但您必须将 options.partitionKey
属性 添加到 readDocument(link, options, callback)
调用中的可选第二个参数。
这也是文档中的一个疏忽,但我认为 partitionKey 需要是一个包含单个元素的数组(例如 {options: {partitionKey: ["myKey"]}}
[更新:我听说它现在可以使用单个分区键值但我自己还没有证实。]
或者,您可以将 options.enableCrossPartitionQuery
属性 设置为 true
,但效率较低。
我正在尝试使用文档 ID 从 DocumentDB 读取单个文档。该集合有四个字段,author
是分区键。
{
"id": string,
"author": string,
"title": string,
"year": int
}
我有两个函数可以读取存储在 DocumentDB 中的记录。 queryCollectionByBookId
按文档 ID 读取单个文档,queryCollectionGetBooks
returns 读取集合中的所有文档。
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
returns 下面的错误信息。
{"Errors":["The partition key supplied in x-ms-partitionkey header has fewer components than defined in the the collection."]}
有没有其他人看到此错误消息并找到解决方法?
这是 node.js 客户端文档中的疏忽,但您必须将 options.partitionKey
属性 添加到 readDocument(link, options, callback)
调用中的可选第二个参数。
这也是文档中的一个疏忽,但我认为 partitionKey 需要是一个包含单个元素的数组(例如 {options: {partitionKey: ["myKey"]}}
[更新:我听说它现在可以使用单个分区键值但我自己还没有证实。]
或者,您可以将 options.enableCrossPartitionQuery
属性 设置为 true
,但效率较低。