Skip to content
Browse files

Merge pull request #92 from padair/master

Support for $exists
  • Loading branch information...
2 parents 2e773c2 + 8a8c021 commit 5e9b23e6a66946b909cfda13b9b7544f53d1447e @jeffjenkins committed
Showing with 10 additions and 0 deletions.
  1. +5 −0 mongoalchemy/query_expression.py
  2. +5 −0 test/test_query_expressions.py
View
5 mongoalchemy/query_expression.py
@@ -193,6 +193,11 @@ def nin(self, *values):
return QueryExpression({
self : { '$nin' : [self.get_type().wrap_value(value) for value in values] }
})
+
+ def exists(self, exists=True):
+ ''' Create a MongoDB query to check if a field exists on a Document.
+ '''
+ return QueryExpression({self: {'$exists': exists}})
def __str__(self):
return self.get_absolute_name()
View
5 test/test_query_expressions.py
@@ -241,6 +241,11 @@ def test_nin():
assert q.nin(T.i, 1, 2, 3).query == {'i' : {'$nin' : [1,2,3]}}, q.nin(T.i, 1, 2, 3).query
assert q.filter(T.i.nin(1, 2, 3)).query == {'i' : {'$nin' : [1,2,3]}}
+def test_exists():
+ q = Query(T, None)
+ assert q.filter(T.i.exists(False)).query == {'i': {'$exists': False}}
+ assert q.filter(T.i.exists(True)).query == {'i': {'$exists': True}}
+
# free-form queries

0 comments on commit 5e9b23e

Please sign in to comment.
Something went wrong with that request. Please try again.