Documentdb 简明教程

DocumentDB - Update Document

在本章中,我们将学习如何更新文档。使用 Azure 门户,您可以通过在文档浏览器中打开文档,并且更新编辑器中的内容(比如一个文本文件)来轻松更新文档。

update document

点击“保存”按钮。现在,如果您需要使用 .Net SDK 更改文档,您可以直接替换它。您无需删除并重新创建它,这不仅繁琐乏味,而且还会更改资源标识符,这是您在修改文档时不希望的。以下是使用 .Net SDK 更新文档的步骤。

让我们看一下以下 ReplaceDocuments 任务,其中我们查询 isNew 属性为真文档,但是我们什么也得不到,因为没有这样的文档。因此,让我们修改早期添加的文档,那些名称以 New Customer 开头的文档。

Step 1 − 向这些文档添加 isNew 属性,并将其值设置为真。

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();
}

Step 2 − 使用相同的 STARTSWITH 查询获取要更新的文档,它给了我们文档,我们以动态对象形式获取它们。

Step 3 − 附加 isNew 属性,并为每个文档将其设置为真。

Step 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