- DocumentDB 教程
- DocumentDB - 首頁
- DocumentDB - 介紹
- DocumentDB - 優勢
- DocumentDB - 環境設定
- DocumentDB - 建立帳戶
- DocumentDB - 連線帳戶
- DocumentDB - 建立資料庫
- DocumentDB - 列出資料庫
- DocumentDB - 刪除資料庫
- DocumentDB - 建立集合
- DocumentDB - 刪除集合
- DocumentDB - 插入文件
- DocumentDB - 查詢文件
- DocumentDB - 更新文件
- DocumentDB - 刪除文件
- DocumentDB - 資料建模
- DocumentDB - 資料型別
- DocumentDB - 限制記錄
- DocumentDB - 對記錄進行排序
- DocumentDB - 對記錄進行索引
- DocumentDB - 地理空間資料
- DocumentDB - 分割槽
- DocumentDB - 資料遷移
- DocumentDB - 訪問控制
- DocumentDB - 資料視覺化
- DocumentDB 有用資源
- DocumentDB - 快速指南
- DocumentDB - 有用資源
- DocumentDB - 討論
DocumentDB - 刪除文件
在本章中,我們將學習如何從你的 DocumentDB 帳戶中刪除文件。使用 Azure Portal,你可以透過在 Document Explorer 中開啟文件並單擊“刪除”選項,輕鬆刪除任何文件。
它將顯示確認資訊。現在按“是”按鈕,你將看到該文件不再出現在你的 DocumentDB 帳戶中。
現在,當你想要使用 .Net SDK 刪除文件時。
步驟 1 − 它與我們之前見到的模式相同,我們將首先查詢以獲取每個新文件的 SelfLinks。在此處,我們不會使用 SELECT *,這將返回整個文件,這對我們來說沒有必要。
步驟 2 − 相反,我們只選擇 SelfLinks 到一個列表中,然後我們只需逐個呼叫每個 SelfLink 的 DeleteDocumentAsync,從集合中刪除文件。
private async static Task DeleteDocuments(DocumentClient client) {
Console.WriteLine();
Console.WriteLine(">>> Delete Documents <<<");
Console.WriteLine();
Console.WriteLine("Quering for documents to be deleted");
var sql =
"SELECT VALUE c._self FROM c WHERE STARTSWITH(c.name, 'New Customer') = true";
var documentLinks =
client.CreateDocumentQuery<string>(collection.SelfLink, sql).ToList();
Console.WriteLine("Found {0} documents to be deleted", documentLinks.Count);
foreach (var documentLink in documentLinks) {
await client.DeleteDocumentAsync(documentLink);
}
Console.WriteLine("Deleted {0} new customer documents", documentLinks.Count);
Console.WriteLine();
}
步驟 3 − 現在,讓我們從 CreateDocumentClient 任務呼叫上述 DeleteDocuments。
private static async Task CreateDocumentClient() {
// Create a new instance of the DocumentClient
using (var client = new DocumentClient(new Uri(EndpointUrl), AuthorizationKey)) {
database = client.CreateDatabaseQuery("SELECT * FROM c WHERE c.id =
'myfirstdb'").AsEnumerable().First();
collection = client.CreateDocumentCollectionQuery(database.CollectionsLink,
"SELECT * FROM c WHERE c.id = 'MyCollection'").AsEnumerable().First();
await DeleteDocuments(client);
}
}
執行上述程式碼時,你將收到以下輸出。
***** Delete Documents ***** Quering for documents to be deleted Found 2 documents to be deleted Deleted 2 new customer documents
廣告