首页 文章

使用Node.js通过ID从DocumentDB读取文档失败

提问于
浏览
1

我正在尝试使用文档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."]}

有没有其他人看到此错误消息,并找到了如何解决它?

1 回答

  • 4

    这是对node.js客户端文档的疏忽,但您必须在 readDocument(link, options, callback) 调用中的可选第二个参数中添加 options.partitionKey 属性 .

    这也是文档中的疏忽,但我认为partitionKey需要是一个包含单个元素的数组(例如 {options: {partitionKey: ["myKey"]}} [更新:我自己确认了 . ]

    或者,您可以将 options.enableCrossPartitionQuery 属性设置为 true ,但效率较低 .

相关问题