Ashley Taylor
Ashley Taylor

Reputation: 165

CosmoDB applying a sort by field removes all documents that do not have that field

We are migrating from mongoDB to CosmoDB using the Mongo API. We have encountered the following difference in query behavior around sorting.

Using the CosmoDB mongo API sorting by a field removes all documents that don't have that field. Is it possible to modifying the query to including the nulls to replicate the mongo behavior?

For example if we have the following 2 documents

[{ "id":"p1", "priority":1 },{ "id":"p2" }]

performing: sort({"priority":1})
cosmoDB will return a single result 'p1'.
mongo will return both results in the order 'p2', 'p1', the null documents will be first.

Upvotes: 3

Views: 759

Answers (2)

JoelSebstn
JoelSebstn

Reputation: 580

I had the same problem and got solved after some reading Refer the document... You have to update the indexing policy of the container to change the default way of Cosmos DB sorting!


Sample change

Upvotes: 1

Brando Zhang
Brando Zhang

Reputation: 28007

As far as I know, the null value will not include in the query result sort scan.

Here is a workaround, you could set a not exists field in the sort method to force the engine scan all the data.

Like this:

db.getCollection('brandotestcollections').find().sort({"test": 1, "aaaa":1})

The result is like this:

enter image description here

Upvotes: 1

Related Questions