Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-76979

$sortArray does not provide correct results in case of case-sensitive data

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 6.0.5
    • Component/s: Aggregation Framework
    • Labels:
    • Server Triage
    • ALL
    • Hide

      // Insert records

      db.engineers.insertOne(
         {
            "team":
               [
                  {
                    "name": "pat",
                    "age": 30,
                    "address": { "street": "12 Baker St", "city": "London" }
                  },
                  {
                    "name": "dallas",
                    "age": 36,
                    "address": { "street": "12 Cowper St", "city": "Palo Alto" }
                  },
                  {
                    "name": "charlie",
                    "age": 42,
                    "address": { "street": "12 French St", "city": "New Brunswick" }
                  },
                  {
                    "name": "Dallas",
                    "age": 36,
                    "address": { "street": "12 Cowper St", "city": "Palo Alto" }
                 }
              ]
      })
      

      sort on a field:

      db.engineers.aggregate( [
         { $project:
            {
                _id: 0,
                result:
                  {
                     $sortArray: { input: "$team", sortBy: { name: 1 } }
                  }
            }
         }
      ] )
      

      // Result of the above query 

       

      [
        {
          "result": [
            {
              "address": {
                "city": "Palo Alto ",
                "street": "12 Cowper St"
              },
              "age": 36,
              "name": "Dallas"
            },
            {
              "address": {
                "city": "New Brunswick",
                "street": "12 French St"
              },
              "age": 42,
              "name": "charlie"
            },
            {
              "address": {
                "city": "Palo Alto",
                "street": "12 Cowper St"
              },
              "age": 36,
              "name": "dallas"
            },
            {
              "address": {
                "city": "London",
                "street": "12 Baker St"
              },
              "age": 30,
              "name": "pat"
            }
          ]
        }
      ]
      
      Show
      // Insert records db.engineers.insertOne(    {       "team":          [             {               "name": "pat",               "age": 30,               "address": { "street": "12 Baker St", "city": "London" }             },             {               "name": "dallas",               "age": 36,               "address": { "street": "12 Cowper St", "city": "Palo Alto" }             },             {               "name": "charlie",               "age": 42,               "address": { "street": "12 French St", "city": "New Brunswick" }             },             {               "name": "Dallas",               "age": 36,               "address": { "street": "12 Cowper St", "city": "Palo Alto" }        }        ] }) sort on a field: db.engineers.aggregate( [    { $project:       {           _id: 0,           result:             {                $sortArray: { input: "$team", sortBy: { name: 1 } }             }       }    } ] ) // Result of the above query    [   {     "result": [       {         "address": {           "city": "Palo Alto ",           "street": "12 Cowper St"         },         "age": 36,         "name": "Dallas"       },       {         "address": {           "city": "New Brunswick",           "street": "12 French St"         },         "age": 42,         "name": "charlie"       },       {         "address": {           "city": "Palo Alto",           "street": "12 Cowper St"         },         "age": 36,         "name": "dallas"       },       {         "address": {           "city": "London",           "street": "12 Baker St"         },         "age": 30,         "name": "pat"       }     ]   } ]

      The sorting of array data using `$sortArray` is not correct. 

      You can check the link - https://mongoplayground.net/p/c61hwM85OG1

            Assignee:
            backlog-server-triage [HELP ONLY] Backlog - Triage Team
            Reporter:
            dalmi.aman@gmail.com Amanpreet Singh
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: