以下代码检索具有特定jobId的所有CrawlResult文档。
var result = (from c in documentDb.CreateDocumentQuery<Shared.CrawlResult>(collection.SelfLink)
where c.JobId == jobId
select c);
现在我要删除具有此特定jobId的所有文档。删除我发现的文件的唯一方法是:
documentDb.DeleteDocumentAsync(string documentLink)
但是如何让documentLink执行documentDb.DeleteDocumentAsync()
?
答案 0 :(得分:11)
为此,您需要编写SQL查询,以便可以动态访问Document的内部属性以及CrawlResult。
例如,如下面的代码所示:
class CrawlResult
{
[JsonProperty("jobId")]
public string JobId;
}
private async Task QueryAndDelete(DocumentClient client, string collectionLink)
{
await client.CreateDocumentAsync(collectionLink, new CrawlResult { JobId = "J123" });
await client.CreateDocumentAsync(collectionLink, new CrawlResult { JobId = "J456" });
foreach (Document document in client.CreateDocumentQuery(
collectionLink,
new SqlQuerySpec(
"SELECT * FROM crawlResults r WHERE r.jobId = @jobId",
new SqlParameterCollection(new[] { new SqlParameter { Name = "@jobId", Value = "J123" } })
)))
{
// Optionally, cast to CrawlResult using a dynamic cast
CrawlResult result = (CrawlResult)(dynamic)document;
await client.DeleteDocumentAsync(document.SelfLink);
}
}
答案 1 :(得分:5)
嗯,官方和有效的方式如下:
首先,您应该创建客户端实例
private static DocumentClient Client
{
get
{
if (client == null)
{
string endpoint = ConfigurationManager.AppSettings["endpoint"];
string authKey = ConfigurationManager.AppSettings["authKey"];
Uri endpointUri = new Uri(endpoint);
client = new DocumentClient(endpointUri, authKey);
}
return client;
}
}
接下来,您可以像这样找到单个文档
private static Document GetDocument(string id)
{
return Client.CreateDocumentQuery(Collection.DocumentsLink)
.Where(d => d.Id == id)
.AsEnumerable()
.FirstOrDefault();
}
然后你可以做每个CRUD操作员。在这种情况下,您可以使用此
public static async Task DeleteAsync(string id)
{
Document doc = GetDocument(id);
await Client.DeleteDocumentAsync(doc.SelfLink);
}
总而言之,您可以在获取Document实例后访问文档链接。之后,您可以找到SelfLink属性。
干杯。
答案 2 :(得分:1)
看起来我来不及了,但是我一直在努力。
public async Task Delete(object key)
{
var result = await _client.ReadDocumentAsync(UriFactory.CreateDocumentUri(dbName, nameof(TEntity), key as string));
await _client.DeleteDocumentAsync(result.Resource.SelfLink);
}
希望获得帮助!
答案 3 :(得分:0)
删除特定文档的另一种可能方法如下:
*)首先,您应该创建一个Document客户端实例。
this.client = new DocumentClient(new Uri(m_ConnInfo.EndPointURL), m_ConnInfo.AccountKey);
Uri _docDbUri = new Uri("https://8878d4ed-0ee0-4-321-c9ef.documents.azure.com");
using (var client = new DocumentClient(_docDbUri, m_ConnInfo.AccountKey))
{
try
{
var coll = client.CreateDocumentCollectionQuery(db.CollectionsLink).ToList().First();
var docs = client.CreateDocumentQuery(coll.DocumentsLink);
foreach (var doc in docs)
{
if (doc.Id == "123")
{
client.DeleteDocumentAsync(doc.SelfLink).Wait();
}
}
}
catch (Exception)
{
//ignored
}
}
我希望这个问题对这个问题更有帮助。