DocumentDB - 資料型別



JSON(JavaScript 物件表示法)是一種輕量級的基於文字的開放標準,旨在用於人類可讀的資料交換,並且易於機器解析和生成。JSON 是 DocumentDB 的核心。我們透過網路傳輸 JSON,將 JSON 儲存為 JSON,並索引 JSON 樹,從而允許對完整的 JSON 文件進行查詢。

JSON 格式支援以下資料型別:

序號 型別和描述
1

數字

JavaScript 中的雙精度浮點數格式

2

字串

帶反斜槓轉義的雙引號 Unicode 字串

3

布林值

真或假

4

陣列

一個有序的值序列

5

它可以是字串、數字、true 或 false、null 等。

6

物件

一個無序的鍵值對集合

7

空格

它可以在任何一對標記之間使用

8

空值

讓我們來看一個簡單的 DateTime 型別示例。將出生日期新增到客戶類。

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 進行儲存、檢索和查詢,如下面的程式碼所示。

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

編譯並執行上述程式碼後,建立文件後,您將看到現在已添加出生日期。

**** 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
廣告
© . All rights reserved.