Documentdb 简明教程

DocumentDB - Update Document

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

In this chapter, we will learn how to update the documents. Using Azure portal, you can easily update document by opening the document in Document explorer and updating it in editor like a text file.

update document

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

Click ‘Save’ button. Now when you need to change a document using .Net SDK you can just replace it. You don’t need to delete and recreate it, which besides being tedious, would also change the resource id, which you wouldn’t want to do when you’re just modifying a document. Here are the following steps to update the document using .Net SDK.

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

Let’s take a look at the following ReplaceDocuments task where we will query for documents where the isNew property is true, but we will get none because there aren’t any. So, let’s modify the documents we added earlier, those whose names start with New Customer.

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

Step 1 − Add the isNew property to these documents and set its value to 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();
}

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

Step 2 − Get the documents to be updated using the same STARTSWITH query and that gives us the documents, which we are getting back here as dynamic objects.

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

Step 3 − Attach the isNew property and set it to true for each document.

Step 4 − 调用ReplaceDocumentAsync,传递文档的SelfLink,以及更新后的文档。

Step 4 − Call ReplaceDocumentAsync, passing in the document’s SelfLink, along with the updated document.

现在只需证明它有效,查询isNew等于true的文档。让我们从CreateDocumentClient任务中调用上述查询。

Now just to prove that this worked, query for documents where isNew equaled true. Let’s call the above queries from the CreateDocumentClient task.

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

}

当上文代码被编译和执行时,您将收到如下输出。

When the above code is compiled and executed, you will receive the following output.

**** 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