
- 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 門戶,您可以透過在文件資源管理器中開啟文件並在編輯器中像文字檔案一樣更新它來輕鬆更新文件。

點選“儲存”按鈕。現在,當您需要使用 .Net SDK 更改文件時,您只需替換它即可。您無需刪除並重新建立它,除了繁瑣之外,這還會更改資源 ID,而您在僅修改文件時不希望這樣做。以下是使用 .Net SDK 更新文件的步驟。
讓我們看一下以下 ReplaceDocuments 任務,我們將查詢 isNew 屬性為 true 的文件,但我們將無法獲取任何文件,因為不存在任何文件。因此,讓我們修改之前新增的文件,即名稱以“新客戶”開頭的文件。
步驟 1 - 將 isNew 屬性新增到這些文件並將其值設定為 true。
private async static Task ReplaceDocuments(DocumentClient client) { Console.WriteLine(); Console.WriteLine(">>> Replace Documents <<<"); Console.WriteLine(); Console.WriteLine("Quering for documents with 'isNew' flag"); var sql = "SELECT * FROM c WHERE c.isNew = true"; var documents = client.CreateDocumentQuery(collection.SelfLink, sql).ToList(); Console.WriteLine("Documents with 'isNew' flag: {0} ", documents.Count); Console.WriteLine(); Console.WriteLine("Quering for documents to be updated"); sql = "SELECT * FROM c WHERE STARTSWITH(c.name, 'New Customer') = true"; documents = client.CreateDocumentQuery(collection.SelfLink, sql).ToList(); Console.WriteLine("Found {0} documents to be updated", documents.Count); foreach (var document in documents) { document.isNew = true; var result = await client.ReplaceDocumentAsync(document._self, document); var updatedDocument = result.Resource; Console.WriteLine("Updated document 'isNew' flag: {0}", updatedDocument.isNew); } Console.WriteLine(); Console.WriteLine("Quering for documents with 'isNew' flag"); sql = "SELECT * FROM c WHERE c.isNew = true"; documents = client.CreateDocumentQuery(collection.SelfLink, sql).ToList(); Console.WriteLine("Documents with 'isNew' flag: {0}: ", documents.Count); Console.WriteLine(); }
步驟 2 - 使用相同的 STARTSWITH 查詢獲取要更新的文件,這將為我們提供文件,我們在這裡將其作為動態物件獲取。
步驟 3 - 附加 isNew 屬性併為每個文件將其設定為 true。
步驟 4 - 呼叫 ReplaceDocumentAsync,傳入文件的 SelfLink 以及更新後的文件。
現在,為了證明這一點有效,請查詢 isNew 等於 true 的文件。讓我們從 CreateDocumentClient 任務中呼叫上述查詢。
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 CreateDocuments(client); //QueryDocumentsWithSql(client); //await QueryDocumentsWithPaging(client); //QueryDocumentsWithLinq(client); await ReplaceDocuments(client); } }
編譯並執行上述程式碼後,您將收到以下輸出。
**** Replace Documents **** Quering for documents with 'isNew' flag Documents with 'isNew' flag: 0 Quering for documents to be updated Found 2 documents to be updated Updated document ‘isNew’ flag: True Updated document ‘isNew’ flag: True Quering for documents with 'isNew' flag Documents with 'isNew' flag: 2
廣告