简体   繁体   English

Azure 带参数的 Cosmos DB 存储过程

[英]Azure Cosmos DB Stored Procedure With Parameter

I am new to Azure Cosmos Db and I am working on a simple stored procedure (JavaScript) that needs to return a document if the Id is provided.我是 Azure Cosmos Db 的新手,我正在研究一个简单的存储过程 (JavaScript),如果提供了 ID,则需要返回一个文档。 I don't get "no docs found" when I execute the stored procedure.当我执行存储过程时,我没有得到“找不到文档”。 Bellow is my code:贝娄是我的代码:

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.');
}```

Queries get automatically scoped to the partition key passed to the stored procedure.查询会自动限定到传递给存储过程的分区键。 So your query is partition key = and id =.所以你的查询是分区键=和id=。 When you skip passing the partition key, it is set to undefined as well.当您跳过传递分区键时,它也设置为未定义。

Can you please try to set it via FeedOptions.PartitionKey in .NET or the x-ms-partition-key header?您能否尝试通过FeedOptions.PartitionKeyx-ms-partition-key header 中的 FeedOptions.PartitionKey 进行设置?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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