简体   繁体   中英

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. The collection has four fields, author being the partition key.

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

I have two functions for reading the records stored into DocumentDB. queryCollectionByBookId reads a single document by document id and queryCollectionGetBooks returns all the documents in the collection.

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.

{"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.

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.]

Alternatively, you can set options.enableCrossPartitionQuery property to true but that is less efficient.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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