资源未找到Azure Cosmos DB

问题描述 投票:1回答:1

嗨我们在查询时遇到问题。该文档存在于数据库中。

“消息:{\”错误\“:[\”未找到资源\“]} \ r \ nActivityId:03866338-6596-49b6-8704-1726cb373bfb,请求URI:/ apps / ab277caf-ee90-4cc3-96cb-4d4ec5ae2b13 / services / 17e48284-a3a0-40c5-b5ec-40bd3f207472 / partitions / 27cb7777-5add-4f72-8a73-1fc8fe34e7bf / replicas / 131603393672093060p /,RequestStats :,SDK:Microsoft.Azure.Documents.Common / 1.19.162.2“

数据库中的文档

{
    "consumername": "testconsumer",
    "tablename": "Table1",
    "securityaccount": "v-naagga",
    "logtime": "2018-01-13T21:42:21.3040338-08:00",
    "securitydefinition": {
        "tablename": "table1",
        "ColumnList": {
            "columnname": "name",
            "columndatatype": "string"
        },
        "RowSecurity": {
            "columnname": "address",
            "operator": "operator",
            "condition": "somecondition"
        }
    },
    "id": "15554839-096d-4072-8f38-af2e9c64b452",
    "_rid": "LmUiAONSDQQBAAAAAAAAAA==",
    "_self": "dbs/LmUiAA==/colls/LmUiAONSDQQ=/docs/LmUiAONSDQQBAAAAAAAAAA==/",
    "_etag": "\"00002e04-0000-0000-0000-5a5aedd60000\"",
    "_attachments": "attachments/",
    "_ts": 1515908566
}

下面是抛出此错误的更新方法代码

{
            try
            {
                RequestOptions options = new RequestOptions();
                options.PartitionKey = new PartitionKey(id);
                options.ConsistencyLevel = ConsistencyLevel.Session;
                return await client.ReplaceDocumentAsync(UriFactory.CreateDocumentUri(DatabaseId, SecurityCollectionId, id), item,options).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                Logger.Log(ErrorLevel.Error, ex.Message);
                throw ex;
            }
        }
c# azure azure-cosmosdb
1个回答
1
投票

根据我的观察,我认为您的问题应该是分区键设置错误。

请参阅此official document。您需要提供分区键的值,而不是存储分区键的字段的名称。

例如,我的容器创建如下:

enter image description here

分区键是我的集合的“名称”。您可以检查集合的分区键。

我的文件如下:

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

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

我的partitionkey是'名字',所以这里我有两个分区:'jay'和'jay1'。

所以,在这里你应该将partitionkey属性设置为'jay'或'jay2',而不是'name'。

try
   {
     RequestOptions options = new RequestOptions();
     options.PartitionKey = new PartitionKey("jay");
     options.ConsistencyLevel = ConsistencyLevel.Session;
     return await client.ReplaceDocumentAsync(UriFactory.CreateDocumentUri(DatabaseId, SecurityCollectionId, id), item,options).ConfigureAwait(false);
   }
     catch (Exception ex)
   {
     Logger.Log(ErrorLevel.Error, ex.Message);
     throw ex;
   }

希望它能帮到你。


更新答案:

我创建了一个与您相同的示例文档并成功替换它。 enter image description here

请参考我的测试代码。

using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using System;
using System.Linq;

namespace ConsoleApp2
{
    class Program
    {

        private static DocumentClient client;

        static string endpoint = "***";
        static string key = "***";
        static string database = "***";
        static string collection = "***";
        static void Main(string[] args)
        {

            client = new DocumentClient(new Uri(endpoint), key);

            try
            {
                Sample querysample = client.CreateDocumentQuery<Sample>(
                UriFactory.CreateDocumentCollectionUri(database, collection))
                .Where(so => so.id == "1")
                .AsEnumerable()
                .First();

                Console.WriteLine(querysample.tablename);

                querysample.tablename = "Table2";

                RequestOptions options = new RequestOptions();
                options.PartitionKey = new PartitionKey("1");
                options.ConsistencyLevel = ConsistencyLevel.Session;
                var result =  client.ReplaceDocumentAsync(UriFactory.CreateDocumentUri(database, collection, "1"), querysample, options).Result;
            }
            catch (Exception ex)
            {
                throw ex;
            }


            Console.ReadLine();
        }
    }

    public class Sample
    {
        public string id { get; set; }
        public string tablename { get; set; }
    }
}

id是我的分区键,值是'1'。您能否检查我们的代码之间的差异?

如有任何疑虑,请告诉我。

© www.soinside.com 2019 - 2024. All rights reserved.