How would I order by multiple fields in CosmosDB and still use CosmosDB's pagination in one query?

cosmos db order by date
the order by query does not have a corresponding composite index that it can be served from
cosmos db order by desc
cosmos db order by datetime
cosmos db order by _ts
cosmos db order by group by
cosmos db order by timestamp
cosmos db order-by over correlated collections is not supported

Say I have a collection with documents like this:

     "ClientName": "7-Eleven",
     "OtherName": "Other",
     "Cost": "1200"

I would like to query it to order by Cost descending then ClientName and then OtherName. The query language doesn't seem to support ordering by more than one field. I'm afraid that what I'm asking is impossible without doing multiple separate queries or without writing a stored procedure. If that's the case, how would you do this using a manner other than one query?

Sorry, at time of writing (13 May, 2018), the SQL API only supports a single field in order by clauses. The SQL Query language itself has support for ordering by multiple fields. That is shown below and you can read more in the docs here.

ORDER BY <sort_specification>  
<sort_specification> ::= <sort_expression> [, <sort_expression>]  
<sort_expression> ::= <scalar_expression> [ASC | DESC]

However, the docs go on to say that only a single field is supported by CosmosDB at this time:

While the query grammar supports multiple order by properties, the Azure Cosmos DB query runtime supports sorting only against a single property, and only against property names, i.e., not against computed properties.

The good news, however, is that it seems that work is underway to support multiple sort fields.

ORDER BY clause in Azure Cosmos DB,​OrderByClause This work is still currently in progress and actively working on it​. This dosent work, following query fails with error: Multiple order-by Hi, I just landed on this page as I tried to implement multi order by in my application. Remarks. The ORDER BY clause requires that the indexing policy include an index for the fields being sorted. The Azure Cosmos DB query runtime supports sorting against a property name and not against computed properties. Azure Cosmos DB supports multiple ORDER BY properties. In order to run a query with multiple ORDER BY properties,

It is now possible to ORDER BY on several fields on CosmosDB by manually building the SQL statements. Here is the link to the Microsoft doc for composite indexes.

Also, to have the opportunity to do everything in LINQ, go vote for this idea on the Azure Feedback Forum.


With the new CosmosV3 nuget, it's now possible to order by more than 1 field. Simply use the ThenBy or ThenByDescending after your OrderBy method and it should work fine.

Allow multi Order By – Customer Feedback for ACE Community , Lately I've been working on a project using Cosmos DB on Azure. An example: if there are 2 documents in the database which represent a User, SELECT * FROM Orders WHERE CustomerId in (@customerIds) In traditional SQL the like keyword can be used to filter a string field on a certain value. Guide to use ORDER BY in Azure Cosmos DB Queries you try to ORDER By id anymore. You still need to use ORDER BY with indexed properties. use multiple

I ended up doing all the sorting I needed to on these fields before inserting into CosmosDB. I added a field called "SortOrder" that I then went and used in the OrderBy. It was nice because I was able to keep all the business logic in the business layer this way.

Things I wish I knew before I got started with CosmosDB, Another good thing is, each request to Azure Cosmos DB returns used RUs to you For a Multi-Tenant app, TenantId can be used as a partition key for most of If you still need to run cross partition queries, you need to explicitly specify it in​  started · AdminAzure Cosmos DB Team (Product Manager, Microsoft Azure) responded. This work is still currently in progress and actively working on it. Stay tuned for this feature in an upcoming release.

Introduction to Azure Cosmos DB, Azure Cosmos DB uses an inverted index, representing your data in a tree form. Still within the FoodCollection node, select the Scale & Settings link. Queries with multiple properties in the ORDER BY clause require a composite index. Still In the Database id field, select the Create new option and enter the value  From trees to property paths. The reason why Azure Cosmos DB transforms items into trees is because it allows properties to be referenced by their paths within those trees. To get the path for a property, we can traverse the tree from the root node to that property, and concatenate the labels of each traversed node.

Indexing in Azure Cosmos DB, Azure Cosmos DB SQL API accounts provide support for querying items You can use many rich query capabilities such as sub-queries or aggregation functions but still retain the many advantages of modeling data in a NoSQL database. If no ORDER BY clause is used, it will result in a deterministic order of values. An Azure Cosmos DB SQL query used for retrieving multiple documents. The property supports runtime bindings, as in this example: SELECT * FROM c where c.departmentId = {departmentId} . Don't set both the id and sqlQuery properties.

Querying in Azure Cosmos DB, Similar to a technique we use in SQL Server, with Azure Cosmos DB, we can update full documents in an object-level that may have many fields change and we want to We still see the _id field, even though we didn't specify it. Sometimes we work with data sets like this – we have fields for order and  The following examples illustrate how some of the standard LINQ query operators translate to Cosmos DB queries. Select operator. The syntax is input.Select(x => f(x)), where f is a scalar expression. Select operator, example 1: LINQ lambda expression. input.Select(family => family.parents[0].familyName); SQL