-
Type:
Improvement
-
Resolution: Won't Fix
-
Priority:
Major - P3
-
None
-
Affects Version/s: None
-
Component/s: None
-
None
-
Query Optimization
-
None
-
None
-
None
-
None
-
None
-
None
-
None
> db.createCollection("special", {collation: { locale: "en", strength: 1 }}) > db.special.explain().find({_id: "123"}, null, { collation: {locale: "simple" }}) <snip …> winningPlan … COLLSCAN
The above query doesn’t use the collection’s _id index because of a locale mismatch. In the case of a locale:simple query, though, the matched documents will always be a subset of those that would match with the _id index. It’s thus possible to match along the _id index then apply a binary filter.
Currently this is possible with the following workaround:
db.special.explain().find({
$and: [
{_id: "123"},
{$expr: {
$eq: [
{$_internalKeyStringValue: { input: "$_id" }},
{$_internalKeyStringValue: { input: {$literal: "123" }}},
],
}},
],
})
… but the above relies on an undocumented operator and is quite a hack besides.
The hack also doesn’t work with upserts since upserts forbid $expr in the filter.