400 error when upsert using Cosmos SP

天涯浪子 提交于 2019-12-13 04:14:14

问题


I'm trying to execute the below SP

function createMyDocument() {
    var collection = getContext().getCollection();

    var doc = {
        "someId": "123134444",
    };
    var options = {};
    options['PartitionKey'] =  ["someId"];

    var isAccepted = collection.upsertDocument(collection.getSelfLink(), doc, options, function (error, resources, options) {

    });

}

and cosmos keeps on complaining that there's something wrong with the partition key

    { code: 400,
      body: '{"code":"BadRequest","message":"Message: {\\"Errors\\":
[\\"PartitionKey extracted from document doesn\'t match the one specified in the header\\"]}
    }

Does anyone have any idea how to pass in the partion key in options so it gets pass this validation ?


回答1:


Figured it out. The error was with how we call the stored proc.

How we were doing it

 client.executeStoredProcedure('dbs/db1/colls/coll-1/sprocs/createMyDocument',
        {},
        {} //Here you have to pass in the partition key

);

How it has to be

     client.executeStoredProcedure('dbs/db1/colls/coll-1/sprocs/createMyDocument',
            {},
            {"partitionKey": "43321"} 
);



回答2:


I think you misunderstand the meaning of partitionkey property in the options[].

For example , my container is created like this:

The partition key is "name" for my collection here. You could check your collection's partition key.

And my documents as below :

{
    "id": "1",
    "name": "jay"
}

{
    "id": "2",
    "name": "jay2"
}

My partitionkey is 'name', so here I have two paritions : 'jay' and 'jay1'.

So, here you should set the partitionkey property to '123134444' in your question, not 'someId'.

More details about cosmos db partition key.

Hope it helps you.



来源:https://stackoverflow.com/questions/48900680/400-error-when-upsert-using-cosmos-sp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!