Skip to content

Commit 8dfb683

Browse files
committed
CombinedSearchExpression
1 parent 5673188 commit 8dfb683

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

django_mongodb_backend/expressions/builtins.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,35 @@ def __init__(self, lhs, connector, rhs, output_field=None):
519519
self.lhs = lhs
520520
self.rhs = rhs
521521

522+
def as_mql(self, compiler, connection):
523+
if self.connector == self.AND:
524+
return CompoundExpression(must=[self.lhs, self.rhs])
525+
if self.connector == self.NEGATION:
526+
return CompoundExpression(must_must=[self.lhs])
527+
raise ValueError(":)")
528+
529+
def __invert__(self):
530+
# SHOULD BE MOVED TO THE PARENT
531+
return self
532+
533+
534+
class CompoundExpression(SearchExpression):
535+
def __init__(self, must=None, must_not=None, should=None, filter=None, score=None):
536+
self.must = must
537+
self.must_not = must_not
538+
self.should = should
539+
self.filter = filter
540+
self.score = score
541+
542+
def as_mql(self, compiler, connection):
543+
params = {}
544+
for param in ["must", "must_not", "should", "filter"]:
545+
clauses = getattr(self, param)
546+
if clauses:
547+
params[param] = [clause.as_mql(compiler, connection) for clause in clauses]
548+
549+
return {"$compound": params}
550+
522551

523552
def register_expressions():
524553
Case.as_mql = case

0 commit comments

Comments
 (0)