问题描述
我正在尝试从Cosmos db容器中删除一行。我只有身份证。 PartitionKey是不同的,即Category列。但是删除时我没有类别值。我是Cosmos DB的新手。有什么方法可以执行此操作吗?
var task = await this.MainContainer.DeleteItemAsync<T>(id,new PartitionKey("Category1"));
解决方法
如果您不知道分区键的值,并且文档中确实有一个分区字段,请使用EnableCrossPartitionQuery
属性启用跨分区查询。
然后,通过Id
查询文档,并获取其SelfLink
属性。使用此SelfLink
值删除您的文档。
代码:
FeedOptions queryOptions = new FeedOptions
{
MaxItemCount = 10,EnableCrossPartitionQuery = true
};
var id = '1';
var queryString = "SELECT * FROM c WHERE c.id= '" + id + "'";
var queryInSql = documentDbClient.CreateDocumentQuery<Document>(
"uri",queryString,queryOptions).AsDocumentQuery();
var document = await queryInSql.ExecuteNextAsync<Document>().ConfigureAwait(false);
//Delete a document using its selfLink property
//To get the documentLink you would have to query for the Document,using CreateDocumentQuery(),and then refer to its .SelfLink property
await documentDbClient.DeleteDocumentAsync(document.AsEnumerable().FirstOrDefault().SelfLink);