How to Sort Special character in MongoDB Using Collation

Viewed 148

I am sorting the collection with alphabetical order while in MongoDB have different sorting rule like number-> caps letters-> small letters. so I am using Collation approach by override caseFirst='upper' but if value contains any special character like @,#,$,^ then sorting order is not based on ASCII value. it came before number but within special character it is random. Sample Data:

[
  {
    "label": "a B"
  },
  {
    "label": "A@B"
  },
  {
    "label": "A&B"
  },
  {
    "label": "a#B"
  }
]

Expected output

[
  {
    "label": "a B"
  },
  {
    "label": "a#B"
  },
  {
    "label": "A&B"
  },
  {
    "label": "A@B"
  }
]

I am using db.collection.aggregate([{sort:{label:1}}],{collation:{locale:en,caseFirst:upper})

0 Answers
Related