DocumentDB - 数据类型
JSON 或 JavaScript 对象表示法是一种轻量级的基于文本的开放标准,专为人类可读的数据交换而设计,也易于机器解析和生成。 JSON 是 DocumentDB 的核心。 我们通过线路传输 JSON,将 JSON 存储为 JSON,并为 JSON 树建立索引,以允许查询完整的 JSON 文档。
JSON格式支持以下数据类型 −
| S.No. | 类型 & 描述 |
|---|---|
| 1 | Number JavaScript 中的双精度浮点格式 |
| 2 | String 带反斜杠转义的双引号 Unicode |
| 3 | Boolean True 或 false |
| 4 | Array 值的有序序列 |
| 5 | Value 可以是字符串、数字、true 或 false、null 等 |
| 6 | Object 键值对的无序集合 |
| 7 | Whitespace 它可以在任何一对令牌之间使用 |
| 8 | Null Empty |
让我们看一个简单的 DateTime 类型示例。 将出生日期添加到客户类别中。
public class Customer {
[JsonProperty(PropertyName = "id")]
public string Id { get; set; }
// 必须可为空,除非为客户端上的新客户生成唯一值
[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

