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

我试图通过使用文档ID从DocumentDB读取单个文档。 集合有四个字段, author是分区键。

 { "id": string, "author": string, "title": string, "year": int } 

我有两个函数来读取存储在DocumentDB中的logging。 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)调用readDocument(link, options, callback)属性添加到可选的第二个参数中。

这也是在文档中的疏忽,但我认为partitionKey需要是一个单一的元素(例如{options: {partitionKey: ["myKey"]}} [分区键的价值,但我还没有证实自己。]

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