2
2
from django .db import NotSupportedError
3
3
from django .db .models import Index
4
4
from django .db .models .expressions import Col
5
+ from django .db .models .fields .related_lookups import In
5
6
from django .db .models .lookups import BuiltinLookup
6
7
from django .db .models .sql .query import Query
7
8
from django .db .models .sql .where import AND , XOR , WhereNode
8
9
9
10
from .query_utils import process_rhs
10
11
12
+ mongo_operators_idx = {
13
+ "exact" : lambda a , b : {a : {"$eq" : b }},
14
+ "gt" : lambda a , b : {a : {"$gt" : b }},
15
+ "gte" : lambda a , b : {a : {"$gte" : b }},
16
+ "lt" : lambda a , b : {a : {"$lt" : b }},
17
+ "lte" : lambda a , b : {a : {"$lte" : b }},
18
+ "in" : lambda a , b : {a : {"$in" : b }},
19
+ }
20
+
11
21
12
22
def _get_condition_mql (self , model , schema_editor ):
13
23
"""Analogous to Index._get_condition_sql()."""
@@ -23,13 +33,21 @@ def builtin_lookup_idx(self, compiler, connection):
23
33
lhs_mql = self .lhs .target .column
24
34
value = process_rhs (self , compiler , connection )
25
35
try :
26
- return connection . mongo_operators_idx [self .lookup_name ](lhs_mql , value )
36
+ return mongo_operators_idx [self .lookup_name ](lhs_mql , value )
27
37
except KeyError :
28
38
raise NotSupportedError (
29
- f"MongoDB does not support the { self .lookup_name } lookup in indexes."
39
+ f"MongoDB does not support the ' { self .lookup_name } ' lookup in indexes."
30
40
) from None
31
41
32
42
43
+ def in_idx (self , compiler , connection ):
44
+ if not connection .features .is_mongodb_6_0 :
45
+ raise NotSupportedError (
46
+ f"MongoDB does not support the { self .lookup_name } lookup in indexes."
47
+ )
48
+ return builtin_lookup_idx (self , compiler , connection )
49
+
50
+
33
51
def where_node_idx (self , compiler , connection ):
34
52
if self .connector == AND :
35
53
full_needed , empty_needed = len (self .children ), 1
@@ -38,13 +56,13 @@ def where_node_idx(self, compiler, connection):
38
56
if self .connector == AND :
39
57
operator = "$and"
40
58
elif self .connector == XOR :
41
- raise NotSupportedError ("Xor in indexes is not supported ." )
59
+ raise NotSupportedError ("MongoDB does not support the 'Xor' lookup in indexes ." )
42
60
else :
43
61
if not connection .features .is_mongodb_6_0 :
44
- raise NotSupportedError ("Or in indexes is not supported ." )
62
+ raise NotSupportedError ("MongoDB does not support the 'Or' lookup in indexes ." )
45
63
operator = "$or"
46
64
if self .negated :
47
- raise NotSupportedError ("Negated field in indexes is not supported ." )
65
+ raise NotSupportedError ("MongoDB does not support negated field in indexes ." )
48
66
children_mql = []
49
67
for child in self .children :
50
68
try :
@@ -77,5 +95,6 @@ def where_node_idx(self, compiler, connection):
77
95
78
96
def register_indexes ():
79
97
BuiltinLookup .as_mql_idx = builtin_lookup_idx
98
+ In .as_mql_idx = in_idx
80
99
Index ._get_condition_mql = _get_condition_mql
81
100
WhereNode .as_mql_idx = where_node_idx
0 commit comments