简体   繁体   English

Azure Cosmos DB SDK 2、查询一个不存在且不存在未知异常的分区键?

[英]Azure Cosmos DB SDK 2, Query a parition key that does not exists without unknow exception?

When we try to run a query to check if the document exists with a partition key we get a exception.当我们尝试运行查询以检查文档是否存在分区键时,我们会遇到异常。

The code:编码:

            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":["An unknown error occurred while processing this request. InnerException = {"Message: {"Errors":["处理此请求时发生未知错误。 If the issue persists, please contact Azure Support: http://aka.ms/azure-support"]}\r\nActivityId: 60dae5e2-c542-462f-bcca-640216dac4d7, Request 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, Number of regions attempted:1\r\nResponseTime: 2020-07-03T15:20:24.3725571Z, StoreResult: StorePhysicalAddress: 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: True, StatusCode: 500, SubStatusCode: 0, RequestCharge: 1, ItemLSN: -1, SessionToken: -1#258197, UsingLocalLSN: True, TransportException: null, ResourceType: Document, OperationType: Query\r\n, SDK: Microsoft.Z3A5 If the issue persists, please contact Azure Support: http://aka.ms/azure-support"]}\r\nActivityId: 60dae5e2-c542-462f-bcca-640216dac4d7, Request 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, Number尝试的区域数:1\r\n响应时间:2020-07-03T15:20:24.3725571Z,StoreResult:StorePhysicalAddress: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:True,StatusCode:500,SubStatusCode:0,RequestCharge:1,ItemLSN:-1,SessionToken:-1#258197 , UsingLocalLSN: True, TransportException: null, ResourceType: Document, OperationType: Query\r\n, SDK: Microsoft.Z3A5 80F142203677F1F0BC30898F63F53Z.Documents.Common/2.9.2, Windows/10.0.18363 documentdb-netcore-sdk/2.10.0"} 80F142203677F1F0BC30898F63F53Z.Documents.Common/2.9.2,Windows/10.0.18363 documentdb-netcore-sdk/2.10.0"}

How can i run this code successfully?如何成功运行此代码? We are using sdk2.我们正在使用 sdk2。

After my testing, the following code hopes to help you.经过我的测试,下面的代码希望对你有所帮助。

Data in my DocumentDB.我的 DocumentDB 中的数据。

在此处输入图像描述

Code running result.代码运行结果。

在此处输入图像描述

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; }
        }
    }
}

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

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