- 文档数据库教程
- DocumentDB - 主页
- DocumentDB - 简介
- DocumentDB - 优点
- DocumentDB - 环境设置
- DocumentDB - 创建帐户
- DocumentDB - 连接帐户
- DocumentDB - 创建数据库
- DocumentDB - 列出数据库
- DocumentDB - 删除数据库
- DocumentDB - 创建集合
- DocumentDB - 删除集合
- DocumentDB - 插入文档
- DocumentDB - 查询文档
- DocumentDB - 更新文档
- DocumentDB - 删除文档
- DocumentDB - 数据建模
- DocumentDB - 数据类型
- DocumentDB - 限制记录
- DocumentDB - 记录排序
- DocumentDB - 索引记录
- DocumentDB - 地理空间数据
- DocumentDB - 分区
- DocumentDB - 数据迁移
- DocumentDB - 访问控制
- DocumentDB - 可视化数据
- DocumentDB 有用资源
- DocumentDB - 快速指南
- DocumentDB - 有用的资源
- DocumentDB - 讨论
DocumentDB - 记录排序
Microsoft Azure DocumentDB 支持使用 SQL over JSON 文档查询文档。您可以在查询中使用 ORDER BY 子句按数字和字符串对集合中的文档进行排序。该子句可以包含可选的 ASC/DESC 参数来指定必须检索结果的顺序。
让我们看一下下面的示例,其中有一个 JSON 文档。
{
"id": "Food Menu",
"description": "Grapes, red or green (European type, such as Thompson seedless), raw",
"tags": [
{
"name": "grapes"
},
{
"name": "red or green (european type"
},
{
"name": "such as thompson seedless)"
},
{
"name": "raw"
}
],
"foodGroup": "Fruits and Fruit Juices",
"servings": [
{
"amount": 1,
"description": "cup",
"weightInGrams": 151
},
{
"amount": 10,
"description": "grapes",
"weightInGrams": 49
},
{
"amount": 1,
"description": "NLEA serving",
"weightInGrams": 126
}
]
}
以下是按降序对结果进行排序的 SQL 查询。
SELECT f.description, f.foodGroup, f.servings[2].description AS servingDescription, f.servings[2].weightInGrams AS servingWeight FROM f ORDER BY f.servings[2].weightInGrams DESC
执行上述查询时,您将收到以下输出。
[
{
"description": "Grapes, red or green (European type, such as Thompson
seedless), raw",
"foodGroup": "Fruits and Fruit Juices",
"servingDescription": "NLEA serving",
"servingWeight": 126
}
]