2 / 4
Jun 2024

Does MongoDB use a different sort sequence order when sorting within a $search operation compared to a normal sort operation?

case 1:

db.collection.aggregate([{ $search: { index: "default_multi_sort", compound: { must: [ { in: { path: "_id", value: [ ObjectId("636cf2849d8e183db53aafca"), ObjectId("636cf2849d8e183db53aafcf"), ObjectId("636cf2849d8e183db53aafcd"), ObjectId("636cf2849d8e183db53aafce"), ], }, }, ], }, sort: { name: 1, }, }, }],, { collation: { locale: "en", numericOrdering: true } })

case 2:

db.collection.aggregate([ { $match: { _id: { $in: [ ObjectId("636cf2849d8e183db53aafca"), ObjectId("636cf2849d8e183db53aafcf"), ObjectId("636cf2849d8e183db53aafcd"), ObjectId("636cf2849d8e183db53aafce"), ], }, }, }, { $sort: { name: 1, }, }, ], { collation: { locale: "en", numericOrdering: true } });```

In the first case, I receive numbers followed by alphabets, while in the second case, I receive a special character, then numbers, and finally alphabets. If this is true, how can I make them behave the same way?

below is my search index definition:
``

{ "mappings": { "dynamic": true, "fields": { "name": [ { "tokenization": "nGram", "type": "autocomplete", "analyzer": "lucene.keyword" }, { "type": "token" } ], } } }
9 days later

Is there no one in this community with any recent knowledge or updates on this topic? I’ve been dealing with this problem for a while now.