11from django .core .exceptions import EmptyResultSet , FullResultSet
22from django .db import NotSupportedError
33from django .db .models import Index
4+ from django .db .models .expressions import Col
5+ from django .db .models .lookups import BuiltinLookup
46from django .db .models .sql .query import Query
57from django .db .models .sql .where import AND , XOR , WhereNode
68
9+ from .query_utils import process_rhs
10+
711
812def _get_condition_mql (self , model , schema_editor ):
913 """Analogous to Index._get_condition_sql()."""
@@ -13,6 +17,14 @@ def _get_condition_mql(self, model, schema_editor):
1317 return where .as_mql_idx (compiler , schema_editor .connection )
1418
1519
20+ def builtin_lookup_idx (self , compiler , connection ):
21+ if not isinstance (self .lhs , Col ):
22+ raise NotSupportedError ("Expressions as indexes are not supported." )
23+ lhs_mql = self .lhs .target .column
24+ value = process_rhs (self , compiler , connection )
25+ return connection .mongo_operators_idx [self .lookup_name ](lhs_mql , value )
26+
27+
1628def where_node_idx (self , compiler , connection ):
1729 if self .connector == AND :
1830 full_needed , empty_needed = len (self .children ), 1
@@ -57,5 +69,6 @@ def where_node_idx(self, compiler, connection):
5769
5870
5971def register_indexes ():
72+ BuiltinLookup .as_mql_idx = builtin_lookup_idx
6073 Index ._get_condition_mql = _get_condition_mql
6174 WhereNode .as_mql_idx = where_node_idx
0 commit comments