0

I am trying to create an Azure Document DB (cosmos DB) partitioned collection using node.

function _getOrCreateCollectionAsync(databaseUrl, collection){
var collectionUrl = `${databaseUrl}/colls/${collection.name}`,
    def = Q.defer();

    console.log("getting collection: " + collectionUrl);

    client.readCollection(collectionUrl, (err, result) => {
        if (err) {
            if (err.code == 404) {
                console.log("collection " + collection.name + " not found... creating...");

                var colOptions = {};
                colOptions.offerThroughput = collection.azureOptions.offerThroughput || 10000; //default is 10k
                colOptions.partitionKey = ["/data"];
                // colOptions.partitionKey = ["data"];
                // colOptions.partitionKey = "/data";
                // colOptions.partitionKey = "data";

                var reqOptions = {
                    id : collection.name
                    //, partitionKey : ["/data"]
                }

                client.createCollection(databaseUrl, reqOptions, colOptions, (err, created) => {
                    if (err) {
                        console.log(err);
                        def.reject(err)
                    } else {
                        def.resolve(created);
                    }
                });
            } else {
                console.log(err);
                def.reject(err);
            }
        } else {
            def.resolve(result);
        }
    });

    return def.promise;
 }

(please ignore the code sloppiness, im still trying to get it to work)

when I run this I am getting this error

.......... working with collection: testCollection
getting collection: dbs/testDb/colls/testCollection
collection testCollection not found... creating...
{ code: 400,
  body: '{"code":"BadRequest","message":"Message: {\\"Errors\\":[\\"x-ms-documentdb-partitionkey header cannot be specified for this request\\"]}\\r\\nActivityId: (snip), Request URI: /apps/(snip)/services/(snip)/partitions/(snip)/replicas/(snip)"}',
  activityId: '(snip)' }

As you can see from above I have different options for defining the partition key and they all return the same result.

I also attempted to add it to the reqOptions by using this example Create Collection in Azure DocumentDB with Different Partition Mode as a guide (its in c#) and that indicated that he partition key needed to be part of the reqOptions object. When I did that i got this error.

.......... working with collection: testCollection
getting collection: dbs/testDb/colls/testCollection
collection testCollection not found... creating...
{ code: 400,
  body: '{"code":"BadRequest","message":"The specified document collection is invalid.\\r\\nActivityId: (snip)"}',
  activityId: '(snip)' }

At this point I am at a loss and any help would be greatly appreciated.

Thanks

2
  • What is the value you're providing in collection.azureOptions.offerThroughput? Is it less than 2500? Commented Sep 27, 2017 at 15:52
  • i started at 400, but i read in the doc i posted, it had to be over 10,000. I put 10,100 but I got another error saying that the value was too high. So for now I am using 10,000 Commented Sep 27, 2017 at 18:21

1 Answer 1

1

As Cosmos DB REST API documentation stated, partition key must be specified as an object and put in the request body.

So, please change your code as below:

var colOptions = {};
colOptions.offerThroughput = collection.azureOptions.offerThroughput || 10000; //default is 10k

var reqOptions = {
    id: collection.name,
    partitionKey : { paths: ["/data"], kind: "Hash" }
}

client.createCollection(databaseUrl, reqOptions, colOptions, (err, created) => {
    if (err) {
        console.log(err);
        def.reject(err)
    } else {
        def.resolve(created);
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

thank you so much, i looked for 2 days for documentation that would help but I was focusing on node js docs... sometimes you need to just go to the source. Let me test it out...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.