
- DocumentDB Tutorial
- DocumentDB - Home
- DocumentDB - Introduction
- DocumentDB - Advantages
- DocumentDB - Environment Setup
- DocumentDB - Create Account
- DocumentDB - Connect Account
- DocumentDB - Create Database
- DocumentDB - List Databases
- DocumentDB - Drop Databases
- DocumentDB - Create Collection
- DocumentDB - Delete Collection
- DocumentDB - Insert Document
- DocumentDB - Query Document
- DocumentDB - Update Document
- DocumentDB - Delete Document
- DocumentDB - Data Modeling
- DocumentDB - Data Types
- DocumentDB - Limiting Records
- DocumentDB - Sorting Records
- DocumentDB - Indexing Records
- DocumentDB - Geospatial Data
- DocumentDB - Partitioning
- DocumentDB - Data Migration
- DocumentDB - Access Control
- DocumentDB - Visualize Data
- DocumentDB Useful Resources
- DocumentDB - Quick Guide
- DocumentDB - Useful Resources
- DocumentDB - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
DocumentDB - Sorting Records
Microsoft Azure DocumentDB supports querying documents using SQL over JSON documents. You can sort documents in the collection on numbers and strings using an ORDER BY clause in your query. The clause can include an optional ASC/DESC argument to specify the order in which results must be retrieved.
Let’s take a look at the following example in which we have a JSON document.
{ "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 } ] }
Following is the SQL query to sort the result in a descending order.
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
When the above query is executed, you will receive the following output.
[ { "description": "Grapes, red or green (European type, such as Thompson seedless), raw", "foodGroup": "Fruits and Fruit Juices", "servingDescription": "NLEA serving", "servingWeight": 126 } ]
Advertisements