1

The following documentation from https://learn.microsoft.com/en-us/azure/cosmos-db/sql/sql-query-order-by#documents-with-missing-fields says (emphasis mine):

Queries with ORDER BY that are run against containers with the default indexing policy will not return documents where the sort property is undefined. If you would like to include documents where the sort property is undefined, you should explicitly include this property in the indexing policy.

There is also the this question from 2019 that asks about ordering by a properties that do not exist: Cosmos DB queries - using ORDER BY when a property does not exist in all documents

With the accepted answer

Currently, ORDER BY works off of indexed properties, and missing values are not included in the result of a query using ORDER BY.

There are no updates (that I can find) on either https://devblogs.microsoft.com/cosmosdb/ or https://azure.microsoft.com/en-us/updates/?query=Cosmos%20DB that mention changes to this behaviour.

Yet when running the query SELECT * FROM c ORDER BY c.DoesNotExist against a Cosmos DB running in Azure I get results back, but if I run it in the latest Cosmos Emulator (version 2.14.6.0) it does not return any results. When removing the ORDER BY c.DoesNotExist statement the emulator will then return results.

When running the query for a property that sometimes exists, the emulator will return only items where the property exists, but the Azure instance will return items will "missing" values first.

E.g. this query in Azure SELECT c.SometimesExists FROM c ORDER BY c.SometimesExists asc will return

[
    {},
    {},
    {},
    {
        "SometimesExists": 11
    },
    {
        "SometimesExists": 17
    },
    {
        "SometimesExists": 22
    },
    {
        "SometimesExists": 43
    }
]

but the emulator returns

[
    {
        "SometimesExists": 11
    },
    {
        "SometimesExists": 17
    },
    {
        "SometimesExists": 22
    },
    {
        "SometimesExists": 43
    }
]

The indexing policies on both the emulator and Azure Cosmos DB instances have not changed and use the following config:

{
    "indexingMode": "consistent",
    "automatic": true,
    "includedPaths": [
        {
            "path": "/*"
        }
    ],
    "excludedPaths": [
        {
            "path": "/\"_etag\"/?"
        }
    ]
}

If there is a change to this behaviour is there a way to either make it revert to the precious behaviour or specify that "missing" values always come last?

0 Answers0