Azure cosmosdb 当PartitionKeyValue和id两个值相同时,使用DeleteItemAsync删除项

Azure cosmosdb 当PartitionKeyValue和id两个值相同时,使用DeleteItemAsync删除项,azure-cosmosdb,azure-cosmosdb-mongoapi,Azure Cosmosdb,Azure Cosmosdb Mongoapi,我正在尝试从CosmosDB集合中删除一个项目。我正在用电话打电话 ItemResponse-response=wait _deviceCapabilityContainerName.DeleteItemAsync(device.deviceId,new PartitionKey(device.deviceId)) 我见过一些帖子做了完全相同的事情,唯一的区别是,在我的例子中,PartitionKey和id的值都是相同的。我不断得到404资源未发现错误。在这种情况下,如何删除记录。我不想删除收藏

我正在尝试从CosmosDB集合中删除一个项目。我正在用电话打电话

ItemResponse-response=wait _deviceCapabilityContainerName.DeleteItemAsync(device.deviceId,new PartitionKey(device.deviceId))


我见过一些帖子做了完全相同的事情,唯一的区别是,在我的例子中,PartitionKey和id的值都是相同的。我不断得到404资源未发现错误。在这种情况下,如何删除记录。我不想删除收藏并重新创建。

最新的

数据格式

代码

使用系统;
使用System.Threading.Tasks;
使用系统配置;
使用System.Collections.Generic;
Net系统;
使用Microsoft.Azure.Cosmos;
使用System.Linq;
命名空间宇宙数据库
{
班级计划
{
// 
公共静态异步任务主(字符串[]args)
{
尝试
{
Console.WriteLine(“开始操作…\n”);
CosmosClient客户端=新的CosmosClient(“https://localhost:8081/“,”C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2NQ9NDUVTQOBD4B8MGYPMBIZNQYMSECAGQY67XIW/Jw==”;
Database Database=wait client.CreateDatabaseIfNotExistsAsync(“项”);
Container=database.GetContainer(“测试”);
//查询项目
FeedIterator FeedIterator=container.GetItemQueryIterator(“从c中选择*);
List concurrentDeleteTasks=新列表();
while(feedIterator.HasMoreResults)
{
FeedResponse res=等待feedIterator.ReadNextAsync();
foreach(资源中的var项目)
{concurrentDeleteTasks.Add(container.DeleteItemAsync(item.id,new PartitionKey(item.deviceid)));
}
}
等待任务.WhenAll(concurrentDeleteTasks.Take(3));
}
渔获物(COSMOSEXCEX)
{
Exception baseException=de.GetBaseException();
WriteLine(“{0}发生错误:{1}”,de.StatusCode,de);
}
捕获(例外e)
{
WriteLine(“错误:{0}”,e);
}    
最后
{
WriteLine(“演示结束,按任意键退出”);
Console.ReadKey();
}
}
公众课堂反应
{
公共字符串id{get;set;}
公共字符串设备ID{get;set;}
公共字符串_rid{get;set;}
公共字符串_self{get;set;}
公共字符串_etag{get;set;}
公共字符串_附件{get;set;}
公共长{get;set;}
}
}
}
PRIVIOUS

您的错误是分区键不是名称,而是分区键的值

container.DeleteItemAsync<response>(item.id, new PartitionKey(item.adress)
container.DeleteItemAsync(item.id,新分区键(item.address))

关于更多细节,您可以。

是的,我知道。但问题是我的对象,deviceId充当Id,它也是PartitionKey,我在我的对象中没有单独的Id。@nikhil可能没有办法。我以前试过,发现Id是必填字段,
Partition key:/ids
也是必填字段。它是rec建议将Partion键的
id
修改为
ids
,将
value
修改为
“\uu”+value
。试试看。我想我需要修改这些对象。这些对象不是我创建的。我不知道他们之前是如何在没有“id”的情况下添加数据的字段。无论如何,谢谢你的回答。@nikhil上一个答案并不严格。我认为只要记录能够成功添加,就必须有办法删除它。你可以先看看我更新的答案。如果方便的话,你能提供你的数据格式吗?请不要包含敏感信息。我将继续研究我会告诉你这是否可行。我能够解决这个问题。之前从DB
id
获取响应时,字段没有设置(因为模型是如何设计的).为了设置id信息,我在模型中做了一些更改。之后,我的代码与您提到的代码类似,并按预期工作。
using System;
using System.Threading.Tasks;
using System.Configuration;
using System.Collections.Generic;
using System.Net;
using Microsoft.Azure.Cosmos;
using System.Linq;

namespace CosmosDB
{
    class Program
    {
        // <Main>
        public static async Task Main(string[] args)
        {
            try
            {
                Console.WriteLine("Beginning operations...\n");
                CosmosClient client = new CosmosClient("https://localhost:8081/", "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==");
                Database database = await client.CreateDatabaseIfNotExistsAsync("Items");
                Container container = database.GetContainer("test");
                // Query for an item
                FeedIterator<response> feedIterator = container.GetItemQueryIterator<response>("SELECT * FROM c");

                List<Task> concurrentDeleteTasks = new List<Task>();

                while (feedIterator.HasMoreResults)
                {
                    FeedResponse<response> res = await feedIterator.ReadNextAsync();
                    foreach (var item in res)
                    {                            concurrentDeleteTasks.Add(container.DeleteItemAsync<response>(item.id, new PartitionKey(item.deviceid)));
                    }
                }
                await Task.WhenAll(concurrentDeleteTasks.Take(3));
            }
            catch (CosmosException de)
            {
                Exception baseException = de.GetBaseException();
                Console.WriteLine("{0} error occurred: {1}", de.StatusCode, de);
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: {0}", e);
            }    
            finally
            {
                Console.WriteLine("End of demo, press any key to exit.");
                Console.ReadKey();
            }
        }
        public class response
        {
            public string id { get; set; }
            public string deviceid { get; set; }
            public string _rid { get; set; }
            public string _self { get; set; }
            public string _etag { get; set; }
            public string _attachments { get; set; }
            public long _ts { get; set; }
            }
        }
    }
container.DeleteItemAsync<response>(item.id, new PartitionKey(item.adress)