Documentdb 简明教程

DocumentDB - Data Types

JSON 或 JavaScript 对象表示法是一种轻量级的基于文本的开放标准,旨在实现人类可读的数据交换,并且机器也易于解析和生成。JSON 是 DocumentDB 的核心。我们在网络上传输 JSON,我们将 JSON 存储为 JSON,并且我们对 JSON 树进行索引,以便针对完整的 JSON 文档进行查询。

JSON or JavaScript Object Notation is a lightweight text-based open standard designed for human-readable data interchange and also easy for machines to parse and generate. JSON is at the heart of DocumentDB. We transmit JSON over the wire, we store JSON as JSON, and we index the JSON tree allowing queries on the full JSON document.

JSON 格式支持以下数据类型 -

JSON format supports the following data types −

S.No.

Type & Description

1

Number Double-precision floating-point format in JavaScript

2

String Double-quoted Unicode with backslash escaping

3

Boolean True or false

4

Array An ordered sequence of values

5

Value It can be a string, a number, true or false, null, etc.

6

Object An unordered collection of key:value pairs

7

Whitespace It can be used between any pair of tokens

8

Null Empty

让我们看一个简单的例子 DateTime 类型。为客户类添加出生日期。

Let’s take a look at a simple example DateTime type. Add birth date to the customer class.

public class Customer {
   [JsonProperty(PropertyName = "id")]
   public string Id { get; set; }

   // Must be nullable, unless generating unique values for new customers on client
   [JsonProperty(PropertyName = "name")]
   public string Name { get; set; }

   [JsonProperty(PropertyName = "address")]
   public Address Address { get; set; }

   [JsonProperty(PropertyName = "birthDate")]
   public DateTime BirthDate { get; set; }
}

我们能用 DateTime 存储、检索以及查询,如下代码所示。

We can store, retrieve, and query using DateTime as shown in the following code.

private async static Task CreateDocuments(DocumentClient client) {
   Console.WriteLine();
   Console.WriteLine("**** Create Documents ****");
   Console.WriteLine();

   var document3Definition = new Customer {
      Id = "1001",
      Name = "Luke Andrew",

      Address = new Address {
         AddressType = "Main Office",
         AddressLine1 = "123 Main Street",
         Location = new Location {
            City = "Brooklyn",
            StateProvinceName = "New York"
         },
         PostalCode = "11229",
         CountryRegionName = "United States"
      },

      BirthDate = DateTime.Parse(DateTime.Today.ToString()),
   };

   Document document3 = await CreateDocument(client, document3Definition);
   Console.WriteLine("Created document {0} from typed object", document3.Id);
   Console.WriteLine();
}

当上述代码编译并执行时,并且创建了文档后,您将会看到现已添加出生日期。

When the above code is compiled and executed, and the document is created, you will see that birth date is added now.

**** Create Documents ****
Created new document: 1001
{
   "id": "1001",
   "name": "Luke Andrew",
   "address": {
      "addressType": "Main Office",
      "addressLine1": "123 Main Street",
      "location": {
         "city": "Brooklyn",
         "stateProvinceName": "New York"
      },
      "postalCode": "11229",
      "countryRegionName": "United States"
   },
   "birthDate": "2015-12-14T00:00:00",
   "_rid": "Ic8LAMEUVgAKAAAAAAAAAA==",
   "_ts": 1450113676,
   "_self": "dbs/Ic8LAA==/colls/Ic8LAMEUVgA=/docs/Ic8LAMEUVgAKAAAAAAAAAA==/",
   "_etag": "\"00002d00-0000-0000-0000-566efa8c0000\"",
   "_attachments": "attachments/"
}
Created document 1001 from typed object