当 PartitionKeyValue 和 id 两个值相同时使用 DeleteItemAsync 删除项目
Delete an item using DeleteItemAsync when PartitionKeyValue and id both values are same
我正在尝试从 CosmosDB collection 中删除一个项目。我打电话使用
ItemResponse<Device> response = await _deviceCapabilityContainerName.DeleteItemAsync<Device>(device.deviceId, new PartitionKey(device.deviceId));
我看到过完全相同的帖子,唯一不同的是,在我的例子中,PartitionKey 和 id 的值都相同。我不断收到 404 Resource Not found 错误。在这种情况下如何删除记录。我不想删除 collection 和 re-create。
最新
数据格式。
{
"id": "002",
"deviceid": "002",
"_rid": "Jx1nAMAZtRYEAAAAAAAAAA==",
"_self": "dbs/Jx1nAA==/colls/Jx1nAMAZtRY=/docs/Jx1nAMAZtRYEAAAAAAAAAA==/",
"_etag": "\"00000000-0000-0000-5a4b-b12502fe01d6\"",
"_attachments": "attachments/",
"_ts": 1594778419
}
代码.
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)
更多详情,您可以。
我正在尝试从 CosmosDB collection 中删除一个项目。我打电话使用
ItemResponse<Device> response = await _deviceCapabilityContainerName.DeleteItemAsync<Device>(device.deviceId, new PartitionKey(device.deviceId));
我看到过完全相同的帖子,唯一不同的是,在我的例子中,PartitionKey 和 id 的值都相同。我不断收到 404 Resource Not found 错误。在这种情况下如何删除记录。我不想删除 collection 和 re-create。
最新
数据格式。
{
"id": "002",
"deviceid": "002",
"_rid": "Jx1nAMAZtRYEAAAAAAAAAA==",
"_self": "dbs/Jx1nAA==/colls/Jx1nAMAZtRY=/docs/Jx1nAMAZtRYEAAAAAAAAAA==/",
"_etag": "\"00000000-0000-0000-5a4b-b12502fe01d6\"",
"_attachments": "attachments/",
"_ts": 1594778419
}
代码.
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)
更多详情,您可以