【问题标题】:unable to delete document from partitioned collection documentdb无法从分区集合 documentdb 中删除文档
【发布时间】:2018-01-17 10:26:33
【问题描述】:

我在 azure cosmos db 中使用分区集合。 我将文档插入集合中。我正在尝试使用以下代码删除文档,但它没有被删除。抛出以下异常。

internal static bool DeleteDocumentUnderPartition(AzureDocumentDbCollectionKind collectionKind, string partitionKey, string id)
    {
        var doc = DocumentClient.CreateDocumentQuery(GetCollectionLink(collectionKind), new FeedOptions
        {
            PartitionKey = new PartitionKey(partitionKey)
        }).AsEnumerable().FirstOrDefault(x => x.Id.Equals(id));
        DocumentClient.DeleteDocumentAsync(doc.SelfLink);            
        return true;
    }

传递的 partitionKey 的值:“4c1429ca58f84e86a3f2e3d9ba1b74de”
传递的 id 值:“aa20ea966258492792864c3817313b2a”

异常 -->

DocumentClientException:消息:{“错误”:[“找不到资源”]} ActivityId:96696cde-3ea3-41fb-bc9c-70dfd8cfac36,请求 URI: /apps/9dee6cc4-be09-426e-b153-f213b908337a/services/e61dcc20-86c2-4751-b4b7-53fd686d04ae/partitions/42db755a-bded-4fe9-bd4c-3561fae0aaa9/replicas/131599878492s 请求统计:

响应时间:2018-01-18T10:20:07.5146574Z,StoreReadResult: 存储物理地址: rntbd://100.114.47.168:13700 /应用/ 9dee6cc4-be09-426e-B153-f213b908337a /服务/ e61dcc20-86c2-4751-b4b7-53fd686d04ae /分区/ 42db755a-bded-4fe9-bd4c-3561fae0aaa9 /复制/ 131599878429038582s, LSN:177530,GlobalCommittedLsn:177530,PartitionKeyRangeId:, IsValid:True,StatusCode:0,IsGone:False,IsNotFound:True, RequestCharge:1,ItemLSN:-1,ResourceType:集合, 操作类型:读取

响应时间:2018-01-18T10:20:07.5146574Z,StoreReadResult: 存储物理地址: rntbd://100.114.45.40:13700 /应用/ 9dee6cc4-be09-426e-B153-f213b908337a /服务/ e61dcc20-86c2-4751-b4b7-53fd686d04ae /分区/ 42db755a-bded-4fe9-bd4c-3561fae0aaa9 /复制/ 131606373510679694s, LSN:177530,GlobalCommittedLsn:177530,PartitionKeyRangeId:, IsValid:True,StatusCode:0,IsGone:False,IsNotFound:True, RequestCharge:1,ItemLSN:-1,ResourceType:集合, 操作类型:读取

但是,从另一个集合(非分区集合)中删除文档对我有用。 任何帮助将不胜感激。

【问题讨论】:

  • 你确定Database.IdcollectionKind.ToString()对应一个数据库和集合?
  • @juunas,是的!我能够从同一个分区集合中检索文档。
  • 您为partitionKey 传递的值是多少?
  • @GauravMantri 这是一个字符串值(一些 guid 值)映射到创建集合时配置的分区键

标签: c# azure azure-cosmosdb


【解决方案1】:

我尝试使用如下示例代码重现您的问题,但失败了。

using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using System;

namespace ConsoleApp1
{
    class Program
    {
        private static DocumentClient client;

        static void Main(string[] args)
        {

            client = new DocumentClient(new Uri("***"), "***");

            var docUri = UriFactory.CreateDocumentUri("db", "collpart", "1");
            var reqOptions = new RequestOptions { PartitionKey = new PartitionKey("1") };
            client.DeleteDocumentAsync(docUri, reqOptions).Wait();

            //block
            Console.ReadLine();
        }
    }
}

我在分区集合中的文档(id 是我的分区键)就像:

我认为分区键可能是问题的关键。您需要提供分区键的值,而不是存储分区键的字段名称。

或者如果可以正确删除文档,您可以尝试创建一个简单的新分区集合测试。

希望对你有帮助。


更新答案:

我认为你误解了FeedOptionspartitionkey 属性的含义。

例如,我的容器是这样创建的:

这里的分区键是我收藏的“名称”。您可以检查您的收藏的分区键。

我的文件如下:

{
    "id": "1",
    "name": "jay"
}

{
    "id": "2",
    "name": "jay2"
}

我的partitionkey'name',所以这里我有两个分区:'jay''jay1'。 p>

因此,您应该在这里将partitionkey 属性设置为“jay”或“jay2”,而不是“name”。

如果您没有设置错误的分区键,请分享您的分区键设置和您要删除的文档的详细信息? (请隐藏敏感信息,谢谢)

【讨论】:

  • 感谢您的回复。我已经更新了代码 sn-p。我传递的是分区键 (4c1429ca58f84e86a3f2e3d9ba1b74de) 的值,而不是字段名本身。然而它失败了。
  • @VigneshT 您是否尝试过像我在答案中发布的那样简单地创建新的分区集合?
  • 是的。没有错误。尚未尝试使用新的分区集合,将尝试。
  • @VigneshT 好的,请尝试一下,以便我们确定问题来自代码或数据库。
  • 尝试使用新的分区集合并且工作正常。但是对于现有的集合,它会引发我在上面更新的错误
猜你喜欢
  • 2017-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-10
相关资源
最近更新 更多