【问题标题】:Azure Cosmos DB SDK 2, Query a parition key that does not exists without unknow exception?Azure Cosmos DB SDK 2,查询不存在且无未知异常的分区键?
【发布时间】:2020-07-03 15:24:50
【问题描述】:

当我们尝试运行查询以检查文档是否存在带有分区键的情况时,我们会遇到异常。

代码:

            var exists = GetQuery(new QueryOptions() { PartitionKey = customer.Id })
                .Where(x => x.Customer.Id == customer.Id && x.Period == period).Select(x => x.Id).Take(1)
                .AsEnumerable().FirstOrDefault() != null;
            if (exists)
            {
                //No insert is made.
                return null;
            }

InnerException = {"Message: {"Errors":["发生未知错误 在处理这个请求时。如果问题仍然存在,请联系 Azure 支持:http://aka.ms/azure-support"]}\r\nActivityId: 60dae5e2-c542-462f-bcca-640216dac4d7,请求 URI: /apps/DocDbApp/services/DocDbServer11/partitions/a4cb4957-38c8-11e6-8106-8cdcd42c33be/replicas/1p/, RequestStats: \r\nRequestStartTime: 2020-07-03T15:20:24.3705569Z, RequestEndTime:2020-07-03T15:20:24.3725571Z,区域数 尝试:1\r\n响应时间:2020-07-03T15:20:24.3725571Z, 存储结果:存储物理地址: rntbd://127.0.0.1:10253/apps/DocDbApp/services/DocDbServer11/partitions/a4cb4957-38c8-11e6-8106-8cdcd42c33be/replicas/1p/, LSN:258197,GlobalCommittedLsn:-1,PartitionKeyRangeId:0,IsValid: 真,StatusCode:500,SubStatusCode:0,RequestCharge:1,ItemLSN: -1, SessionToken: -1#258197, UsingLocalLSN: True, TransportException: null, ResourceType: Document, OperationType: Query\r\n, SDK: Microsoft.Azure.Documents.Common/2.9.2、Windows/10.0.18363 documentdb-netcore-sdk/2.10.0"}

我怎样才能成功运行这段代码?我们正在使用 sdk2。

【问题讨论】:

  • 请提供更多细节。其余代码看起来如何?
  • 剩下的代码只是来自 documentclient 的 IQueryable,请求选项带有分区键,没有别的。

标签: c# .net azure azure-cosmosdb


【解决方案1】:

经过我的测试,下面的代码希望对你有所帮助。

我的 DocumentDB 中的数据。

代码运行结果。

using System;
using System.Threading.Tasks;
using System.Configuration;
using System.Collections.Generic;
using System.Net;
using System.Linq;
using Microsoft.Azure.Documents.Client;
using Microsoft.Azure.Documents.Linq;
using Microsoft.Azure.Cosmos.Table;
using Microsoft.Azure.Documents;

namespace CosmosGettingStartedTutorial
{
    class Program
    {
        // <Main>
        public static async Task Main(string[] args)
        {
            try
            {
                Console.WriteLine("Beginning operations...\n");
                DocumentClient client = new DocumentClient(new Uri("https://localhost:8081/"), "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==");

                FeedOptions queryOptions = new FeedOptions { PartitionKey = new PartitionKey("wuxi") };


                var testQuery = client.CreateDocumentQuery<TestEntity>(
                UriFactory.CreateDocumentCollectionUri("ToDoList", "jason"), queryOptions)
                .Where(f => f.id == "1").Take(1).AsDocumentQuery().ExecuteNextAsync<TestEntity>()!=null;
                Console.WriteLine(" Check if the document exists: Result is {0}" , testQuery);

                var testQuery2 = client.CreateDocumentQuery<TestEntity>(
                UriFactory.CreateDocumentCollectionUri("ToDoList", "jason"), queryOptions)
                .Where(f => f.id == "1").Take(1).AsDocumentQuery();

                List<TestEntity> retVal = new List<TestEntity>();
                while (testQuery2.HasMoreResults)
                {
                    var results = await testQuery2.ExecuteNextAsync<TestEntity>();
                    retVal.AddRange(results);
                }

                Console.WriteLine(" Item.id is {0} , Item.address is {1}", retVal[0].id,retVal[0].address);

            }
            catch (Exception e)
            {
                Console.WriteLine("Error: {0}", e);
            }
            finally
            {
                Console.WriteLine("End of demo, press any key to exit.");
                Console.ReadKey();
            }
        }
        public class TestEntity : TableEntity
        {
            public TestEntity()
            {
            }

            public TestEntity(string param1, string param2)
            {
                PartitionKey = param1;
                RowKey = param2;
            }
            public string address { get; set; }
            public string id { get; set; }
        }
    }
}

【讨论】:

  • 谢谢,但似乎我在重新创建集合后让它工作了。我想我的模拟器有一个错误,当我在查询选项卡中运行一个简单的“where c.id =”时,有时会出现内部服务器错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多