📜  DocumentDB-排序记录

📅  最后修改于: 2020-11-28 13:49:13             🧑  作者: Mango


Microsoft Azure DocumentDB支持通过JSON文档使用SQL查询文档。您可以使用查询中的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
   }
]