Documentdb-sorting-records

提供:Dev Guides
移動先:案内検索

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
   }
]