Azure Cosmos Db – Part four- Querying the Document DB

Querying the DocumentDb

DocumentDb offers a rich T-SQL like query experience which allow us to query it using the SELECT, FROM and Where Query. It gives all the following queries:

  1. Filtering

As we know filtering refers to refining the data and take out the minimum required data based on the user requirement. If you’re familiar with T-SQL, we use select, from and where Clause.

Let’s us try to see a query where collection name is employee and we need to take out the id, designation, location and BloodGroup from the record and where the employee group is Developers and designation is Software Engineer.





FROM Employee

WHERE Employee.Group = “Developers” and Employee.desigination = “Software Engineer”



Sorting refers to the rearranging the records based on some property in an ascending or descending order. We use the ORDER BY clause on some property and tell it to sort it.  Here we are selecting the Employee designation,location and BloodGroup where the Employee Blood Group is A+  and we are sorting the Records on the basis of the Employee Name in ascending order.


SELECT Employee. desigination,

Employee. location,

Employee. BloodGroup AS BG

FROM Employee

WHERE Employee.EmployeeGroup = “A+”

ORDER BY Employee.Name Asc


TOP refers to taking out N no of Top records from the Database based on the query. Here we are taking out the top 20 records where Blood Group is B+.



Employee. location,

Employee. BloodGroup

FROM Employee

WHERE Employee. BloodGroup= “B+”


4.In & Between

The  BETWEEN condition allows you to easily test if an expression is within a range of values (inclusive). It can be used in a SELECT, INSERT, UPDATE, or DELETE statement.



Employee. location,

Employee. BloodGroup,

FROM Employee

WHERE BETWEEN “05740” AND “07050”


Projection means casting the property into new project JSON Object with modified property names.


“Name”: Employee.Name,

“Role”: Employee.Designation,

“Employee Blood Group”: Employee.BloodGroup

} AS Employee

FROM Employee

WHERE = “21421”


Azure Cosmos DB’s JOIN supports intra-document and self-joins


FROM Employee

JOIN tag IN Employee.tags

WHERE = “09052”


Azure Cosmos DB supports a number of built-in functions for common operations. They cover mathematical functions like ABS, FLOOR and ROUND and type checking functions like IS_ARRAY, IS_BOOL and IS_DEFINED. To find a full list of supported built-in functions, head over to our query page. Run the query below to see these built-in functions in action.


UDF means User Defined Functions.  UDFs can be written in Javascript, then used in SQL Statements. and we will cover how to write a UDF in a separate article. Here just need to understand that how we’re calling the function. Here udf.MATCH is a function which is going to match





FROM Employee

WHERE Employee.BloodGroup = “B+” AND udf.MATCH(Employee.Designation, “Software-Engineer, Senior Software-Engineer”) != null


Spatial data describes the position and shape of objects in space. In most applications, these correspond to objects on the earth, i.e. geospatial data. Spatial data can be used to represent the location of a person, a place of interest, or the boundary of a city, or a lake. Common use cases often involve proximity queries, for e.g., “find all coffee shops near my current location”. Continuing with the Employee example we are looking for employee whose home location is near by the coordinates defined.



FROM Employee E


“type”: “Point”,

“coordinates”: [-122.19, 47.36]

}) < 100 * 1000




Document Db is a no Sql Db but it gives all the T-SQL like query experience. You can run the query on the azure portal or run through the client SDK.  It gives multiple options like Filtering, Sorting, Top, in & Between, Built in, UDF, Joins and Geo Spatial queries.




2 thoughts on “Azure Cosmos Db – Part four- Querying the Document DB

Leave a Reply

Your email address will not be published.