Azure DocumentDB-源自脚本的请求不能引用分区密钥,而不是为其提交客户端请求的分区密钥 [英] Azure DocumentDB - Requests originating from scripts cannot reference partition keys other than the one for which client request was submitted

查看:110
本文介绍了Azure DocumentDB-源自脚本的请求不能引用分区密钥,而不是为其提交客户端请求的分区密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用c#.Net客户端SDK,我按以下方式调用ExecuteStoredProcedureAsync方法:

Using the c# .Net client SDK, I am calling the ExecuteStoredProcedureAsync method as follows:

sproc_response = await client.ExecuteStoredProcedureAsync<Document>(sproc_uri, new RequestOptions { PartitionKey = new PartitionKey( my_partition_key) }, doc_to_create );

存储过程代码如下:

     sproc.Body = @"function( doc ) {

            var collection = getContext().getCollection();    

            var response = getContext().getResponse();    

            function create_doc_callback( err, doc_created, options ) {

               if(err) throw new Error('Error creating document: ' + err.message);                              

               response.setBody( doc_created );

            }

            collection.createDocument( collection.getSelfLink(), doc, {}, create_doc_callback );

        }";

这总是会引发以下异常:

This ALWAYS results in the following exception being thrown:

创建文档时出错:源自脚本的请求不能引用分区密钥,而不是已提交客户端请求的分区密钥。

是没有任何文档或示例清楚地显示了如何使用分区键调用存储过程-显然,我缺少了某些东西!

Is there any documentation or example that clearly shows how to call a stored procedure using a partition key -- obviously, I am missing something!

推荐答案

在Cosmos中创建分区集合时,需要选择一个分区键,该键是文档JSON表示中的路径,该路径将用于将文档放置在正确的分区中。如果您尝试插入不带分区键的文档,则会被归入一个特殊的分区中,该分区用于不带分区键的文档。您指定存储过程应在给定分区的上下文中运行,然后尝试使用该存储过程将文档插入其他分区(未定义的分区)中。确保您的POCO上有一个属性,该属性映射到您为集合中的分区键选择的值。

When you create a partitioned collection in Cosmos you are required to pick a partition key which is a path in the JSON representation of your documents that will be used to place the document in the correct partition. If you're attempting to insert a document with no partition key it's going to get grouped in a special partition for documents with no partition key. You're specifying that the stored procedure should run within the context of a given partition but then trying to use it to insert a document into a different partition (the undefined one). Make sure that your POCO has a property on it that maps to the value you chose for the partition key on your collection.

您清楚知道要分区的内容当您在PartitionKey的RequestOptions中传递值时,放置文档。现在,确保POCO在与创建集合时选择的分区键匹配的属性上包含此值。如果您未提供,则该文档仍将具有自动生成的ID。另外,您可以在处理动态JS对象时将分区键添加到Sproc中的文档中。

You clearly know what partition you'd like to place the document in, as you're passing a value in RequestOptions for PartitionKey. Now ensure that your POCO includes this value on a property that matches the partition key you chose when you created your collection. The document will still have an auto generated Id if you do not provide one. Alternatively, you can just add the partition key to your document inside the Sproc as you're dealing with a dynamic JS object.

doc.partitionKey = my_partition_key

这篇关于Azure DocumentDB-源自脚本的请求不能引用分区密钥,而不是为其提交客户端请求的分区密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆