Skip to content

Commit 7225586

Browse files
committed
CombinedSearchExpression
1 parent d67aec1 commit 7225586

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
@@ -524,6 +524,35 @@ def __init__(self, lhs, connector, rhs, output_field=None):
524524
self.lhs = lhs
525525
self.rhs = rhs
526526

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

528557
def register_expressions():
529558
Case.as_mql = case

0 commit comments

Comments
 (0)