带参数的Azure Cosmos DB存储过程

时间:2019-09-23 16:40:06

标签: javascript azure-cosmosdb

我是Azure Cosmos Db的新手,并且我正在开发一个简单的存储过程(JavaScript),如果提供了ID,则需要返回一个文档。执行存储过程时,我没有“找不到文档”。贝娄是我的代码:

function sample(id) {
    var collection = getContext().getCollection();
    var query = {
    query: "SELECT * FROM c WHERE c.id = \'id\'"
};

    // Query documents and take 1st item.
    var isAccepted = collection.queryDocuments(
        collection.getSelfLink(),query,
    function (err, feed, options) {
        if (err) throw err;

        // Check the feed and if empty, set the body to 'no docs found', 
        // else take 1st element from feed
        if (!feed || !feed.length) {
            var response = getContext().getResponse();
            response.setBody('no docs found');
        }
        else {
            var response = getContext().getResponse();
            var body = { id: id, feed: feed[0] };
            response.setBody(JSON.stringify(body));
        }
    });

    if (!isAccepted) throw new Error('The query was not accepted by the server.');
}```

1 个答案:

答案 0 :(得分:0)

查询将自动限制在传递给存储过程的分区键上。因此,您的查询是partition key =和id =。当您跳过传递分区键时,它也将设置为undefined。

您能否尝试通过.NET中的FeedOptions.PartitionKeyx-ms-partition-key标头设置它?