Can't get Cosmos DB document in DESC order

Phil 96 Reputation points
2020-09-22T04:31:12.89+00:00

I have an API query to a Core (SQL) Cosmos DB collection that doesn't work, and I don't know why. I want to get the newest document in the collection by _ts., the JSON body is ...

{
"query": "SELECT * FROM MyData d ORDER BY d._ts DESC",
"parameters": []
}

Relevant Headers ...

x-ms-version:2018-12-31
x-ms-documentdb-isquery:true
Content-Type:application/query+json
x-ms-documentdb-query-enablecrosspartition:true
x-ms-max-item-count:1

It gets a single document as expected, but it is the first document as if I specified ASC instead of DESC.

I must be overlooking something very silly. please, someone embarrass me with an obvious fix.

Thanks,
Phil

Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,632 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Anurag Sharma 17,606 Reputation points
    2020-09-28T08:17:12.017+00:00

    Hi @Phil , thanks for reverting back with more information.

    This looks like problem with 'Get' request. You need to change it to 'POST while using queries'

    With 'POST' request, I was able to return the latest document as shown in below screenshot:

    28620-image.png

    When I changed it to 'GET', I was getting the first record which is wrong:

    28693-image.png

    Please let us know if you are able to get the proper response after making this change. We can discuss further if you still face the same issue.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.