Reputation: 1916
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
Upvotes: 0
Views: 1340
Reputation: 9950
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);
}
});
Upvotes: 1