none
cursor.sort() does not return sorted result RRS feed

  • Question

  • I am using Cosmos DB with MongoDB API and when I use find() with sort(), the results are not sorted correctly. The order of the returned items do change, but in a strange order. (MongoDB returns correctly sorted result with the same query). Is this a CosmosDB bug?

    Here are the queries and results:

    db.datastore.find({}, {_id: 1, header_timestamp: 1})
    
    { "_id" : "object-bbfg0pritvunrk0bi170", "header_timestamp" : NumberLong(1) }
    { "_id" : "object-bbfg0rjitvunrk0bi180", "header_timestamp" : NumberLong(3) }
    { "_id" : "object-bbfg0s3itvunrk0bi18g", "header_timestamp" : NumberLong(4) }
    { "_id" : "object-bbfg0qritvunrk0bi17g", "header_timestamp" : NumberLong(2) }
    { "_id" : "object-bbfg0tjitvunrk0bi190", "header_timestamp" : NumberLong(5) }
    
    b.datastore.find({}, {_id: 1, header_timestamp: 1}).sort({"header_timestamp": 1})
    
    { "_id" : "object-bbfg0pritvunrk0bi170", "header_timestamp" : NumberLong(1) }
    { "_id" : "object-bbfg0rjitvunrk0bi180", "header_timestamp" : NumberLong(3) }
    { "_id" : "object-bbfg0s3itvunrk0bi18g", "header_timestamp" : NumberLong(4) }
    { "_id" : "object-bbfg0qritvunrk0bi17g", "header_timestamp" : NumberLong(2) }
    { "_id" : "object-bbfg0tjitvunrk0bi190", "header_timestamp" : NumberLong(5) }
    
    db.datastore.find({}, {_id: 1, header_timestamp: 1}).sort({"header_timestamp": -1})
    { "_id" : "object-bbfg0rjitvunrk0bi180", "header_timestamp" : NumberLong(3) }
    { "_id" : "object-bbfg0pritvunrk0bi170", "header_timestamp" : NumberLong(1) }
    { "_id" : "object-bbfg0s3itvunrk0bi18g", "header_timestamp" : NumberLong(4) }
    { "_id" : "object-bbfg0qritvunrk0bi17g", "header_timestamp" : NumberLong(2) }
    { "_id" : "object-bbfg0tjitvunrk0bi190", "header_timestamp" : NumberLong(5) }

    Wednesday, May 9, 2018 1:29 AM

All replies

  • Are you using the MongoDB shell to run these queries? 

    I see the syntax as being slightly different, as referenced in these document:

    MongoDB CRUD Operations > Query Documents

    MongoDB API support for MongoDB features and syntax

    Please let me know what you find.

    Regards,

    Mike

    Wednesday, May 9, 2018 11:30 PM
    Moderator
  • Thanks for your response. Yes, I'm using the MongoDB shell to run those queries.

    Can you tell me which part of the syntax is slightly different? if you mean the find({}, {}) part, the second argument is used to select which field to output on the results, as shown on this document:

    https://docs.mongodb.com/manual/reference/method/db.collection.find/#specify-the-fields-to-return

    else, if you mean the sort() part, cursor.sort() should be supported according to these documents:

    https://docs.microsoft.com/en-us/azure/cosmos-db/mongodb-feature-support#cursor-methods

    https://docs.mongodb.com/manual/reference/method/cursor.sort/

    https://docs.mongodb.com/manual/reference/method/db.collection.find/#order-documents-in-the-result-set

    Thursday, May 10, 2018 4:43 AM
  • I just don't have an environment set-up to test this but, in looking at the mongo documentation I see these examples, which you have posted above.

    Examples:

    db.orders.find().sort( { "item.category": 1, "item.type": 1 } )

    If the sort order is still not right, can you do the following:

    • Go to the document: MongoDB API support for MongoDB features and syntax
    • At the bottom that document is the ability to 'Give product feedback.' It might say 'content feedback'.
    • In that new issue, please detail this specific behavior, including the Mongo sort results versus the Cosmos DB sort results. With this I can push this to the product group in a formalized manner. You can reference this MSDN forum thread as well. 

    Thank you,

    Mike

    Friday, May 11, 2018 10:24 PM
    Moderator
  • I have posted this on the Cosmos DB's feedback page. Here is the link:

    https://feedback.azure.com/forums/263030-azure-cosmos-db/suggestions/34259923-mongo-db-api-cursor-sort-does-not-return-sorte

    Thank you

    Wednesday, May 16, 2018 5:42 AM
  • Have been waiting but there's no response on this forum and azure feedback page, can you tell me about the situation? The sort is still not working as expected.

    Tuesday, June 12, 2018 6:07 AM
  • Do you have an Azure Support plan? If not, can you please provide you Azure Subscription GUID? I can enable a one-time incident ticket to have this issue formally investigated through support. Azure Support is the next step in escalating this issue.

    Thank you,

    Mike

     
    Tuesday, June 12, 2018 3:56 PM
    Moderator
  • You may now submit a support case to Azure Support to have this issue investigated further. Regards, Mike

    +Success. Subscription 271a8267-6dad-4062-86bc-9da99a479094 is now entitled for one time free technical support. Note: This is the first time this subscription has been entitled for one time free technical support. 

    Wednesday, June 13, 2018 4:21 PM
    Moderator