使用CosmosDB SDK V3麻烦查询文档

时间:2019-12-03 14:29:21

标签: c# azure-cosmosdb azure-cosmosdb-sqlapi

我正在尝试更新CosmosDB中的文档,因此我想使用以下查询找到现有文档的ID

string seasonDocumentId = null;

            FeedIterator setIterator = container.GetItemQueryStreamIterator(
                "SELECT * FROM c where c.brand = 'hm' and c.DocumentType = 'Seasons'",
               requestOptions: new QueryRequestOptions()
               {
                   PartitionKey = new PartitionKey("hm"),
                   MaxConcurrency = 1,
                   MaxItemCount = 1
               });

            while (setIterator.HasMoreResults)
            {
                using (ResponseMessage response = await setIterator.ReadNextAsync())
                {
                    using (StreamReader sr = new StreamReader(response.Content))
                    using (JsonTextReader jtr = new JsonTextReader(sr))
                    {
                        JsonSerializer jsonSerializer = new JsonSerializer();
                        SeasonInformation seasons = jsonSerializer.Deserialize<SeasonInformation>(jtr);
                        seasonDocumentId = seasons.id;
                        int i = 0;
                    }
                }
            }

问题是,即使我知道实际的查询是正确的,我也总是获得seasonDocumentId的NULL值,如果我使用数据资源管理器运行查询则返回值,因此我必须在流转换中丢失某些内容,但是我不能放我的手指在指我做错了什么。

模型看起来像这样

class SeasonInformation
    {
        public string id { get; set; }
        [JsonProperty("brand")]
        public string Brand { get; set; }
        public string IntegrationSource { get; set; }
        public string DocumentType { get; set; }
        public string UpdatedDate { get; set; }
        public string UpdatedDateUtc { get; set; }
        public string UpdatedBy { get; set; }
        public JObject OriginalData { get; set; }
    }

CosmosDB中的实际文档如下所示

{
    "id": "f05182d2-0a18-4cc4-b2df-585ad0464abb",
    "brand": "hm",
    "IntegrationSource": "HAPI",
    "DocumentType": "Seasons",
    "UpdatedDate": "3/12/2019 20:02:42",
    "UpdatedDateUtc": "2019-12-03T13:02:42.01Z",
    "UpdatedBy": "HAPI_HM_Seasons",
    "OriginalData": {
        "corporateBrandId": 0,
        "current": "201910"
    },
    "_rid": "xxx==",
    "_self": "dbs/HwVmAA==/colls/xxx=/docs/xxx==/",
    "_etag": "\"3e00bdf8-xxx-0c00-xxx-5de65cef0000\"",
    "_attachments": "attachments/",
    "_ts": 15753xxxx78159
}

1 个答案:

答案 0 :(得分:1)

我为您实现了一个简单的控制台应用程序,它可以为您成功获取ID,请尝试以下代码:

using Microsoft.Azure.Cosmos;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace cosmosDBSDKV3
{
    class Program
    {
        static void Main(string[] args)
        {
            var dbName = "<db name>";
            var containerName = "<container name>";

            CosmosClient client = new CosmosClient("<cosmos db url>", "<key here>");
            Database database = client.GetDatabase(dbName);
            Container container = database.GetContainer(containerName);

            ItemFeed(container).GetAwaiter().GetResult();


            Console.ReadKey();
        }

        private static async Task ItemFeed(Container container)
        {
            FeedIterator<SeasonInformation> setIterator = container.GetItemQueryIterator<SeasonInformation>(
                "SELECT * FROM c where c.brand = 'hm' and c.DocumentType = 'Seasons'",
               requestOptions: new QueryRequestOptions()
               {
                   PartitionKey = new PartitionKey("hm"),
                   MaxConcurrency = 1,
                   MaxItemCount = 1
               });

            List<SeasonInformation> seasonInformations = new List<SeasonInformation>();

            // SQL

            while (setIterator.HasMoreResults)
            {
                int count = 0;
                foreach (SeasonInformation item in await setIterator.ReadNextAsync())
                {
                    count++;
                    seasonInformations.Add(item);
                }
            }

            foreach (SeasonInformation item in seasonInformations) {
                Console.WriteLine(item.id);
                Console.WriteLine(item.Brand);
            }



        }

        class SeasonInformation
        {
            public string id { get; set; }
            [JsonProperty("brand")]
            public string Brand { get; set; }
            public string IntegrationSource { get; set; }
            public string DocumentType { get; set; }
            public string UpdatedDate { get; set; }
            public string UpdatedDateUtc { get; set; }
            public string UpdatedBy { get; set; }
            public JObject OriginalData { get; set; }
        }
    }

}

在我的cosmos db中,这是我的查询结果: enter image description here

代码结果: enter image description here

希望有帮助。