繁体   English   中英

使用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返回集合中的所有文档。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM