开发手册 欢迎您!
软件开发者资料库

DocumentDB SQL - 数组创建

DocumentDB SQL数组创建 - 从概述,选择子句,从子句,子句,操作符,关键字,关键字,值关键字,按顺序子句,迭代,连接,别名,数组创建,简单而简单的步骤学习DocumentDB SQL标量表达式,参数化SQL,内置函数,Linq到SQL转换,JavaScript集成,用户定义函数,复合SQL查询。

在DocumentDB SQL中,Microsoft添加了一个关键功能,借助我们可以轻松创建一个数组.这意味着当我们运行查询时,结果它将创建一个类似于JSON对象的集合数组作为查询结果.

让我们考虑与前面示例中相同的文档.

以下是 AndersenFamily 文件.

{    "id": "AndersenFamily",    "lastName": "Andersen",   "parents": [       { "firstName": "Thomas", "relationship":  "father" },       { "firstName": "Mary Kay", "relationship":  "mother" }    ],       "children": [       {          "firstName": "Henriette Thaulow",          "gender": "female",          "grade": 5,          "pets": [ { "givenName": "Fluffy", "type":  "Rabbit" } ]       }    ],      "location": { "state": "WA", "county": "King", "city": "Seattle" },    "isRegistered": true }

以下是 SmithFamily 文件.

{    "id": "SmithFamily",    "parents": [       { "familyName": "Smith", "givenName": "James" },       { "familyName": "Curtis", "givenName": "Helen" }    ],      "children": [       {         "givenName": "Michelle",          "gender": "female",          "grade": 1       },      {          "givenName": "John",          "gender": "male",          "grade": 7,         "pets": [             { "givenName": "Tweetie", "type": "Bird" }          ]       }    ],       "location": {       "state": "NY",       "county": "Queens",       "city": "Forest Hills"    },      "isRegistered": true }

以下是 WakefieldFamily 文件.

{    "id": "WakefieldFamily",    "parents": [       { "familyName": "Wakefield", "givenName": "Robin" },       { "familyName": "Miller", "givenName": "Ben" }    ],      "children": [       {          "familyName": "Merriam",          "givenName": "Jesse",          "gender": "female",          "grade": 6,         "pets": [             { "givenName": "Charlie Brown", "type": "Dog" },             { "givenName": "Tiger", "type": "Cat" },             { "givenName": "Princess", "type": "Cat" }         ]      },      {          "familyName": "Miller",          "givenName": "Lisa",          "gender": "female",          "grade": 3,         "pets": [             { "givenName": "Jake", "type": "Snake" }          ]       }    ],       "location": { "state": "NY", "county": "Manhattan", "city": "NY" },    "isRegistered": false }

让我们来看一个例子.

数组创建

以下是将返回每个家庭的姓氏和地址的查询.

SELECT f.id AS FamilyName, [f.location.city, f.location.county, f.location.state] AS Address FROM Families f

可以看出,城市,县和州字段用方括号括起来,这将创建一个数组,这个数组名为Address.执行上述查询时,它会产生以下输出.

[    {       "FamilyName": "WakefieldFamily",       "Address": [          "NY",          "Manhattan",          "NY"       ]    },   {       "FamilyName": "SmithFamily",       "Address": [         "Forest Hills",          "Queens",          "NY"       ]    },   {       "FamilyName": "AndersenFamily",       "Address": [          "Seattle",          "King",          "WA"       ]    } ]

市,县,并在上面的输出中的地址数组中添加状态信息.