Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #92 from padair/master

Support for $exists
  • Loading branch information...
commit 5e9b23e6a66946b909cfda13b9b7544f53d1447e 2 parents 2e773c2 + 8a8c021
Jeffrey Jenkins authored
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
Please sign in to comment.
Something went wrong with that request. Please try again.