-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathREADME
61 lines (51 loc) · 1.55 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
This module is designed to make it easier to write queries for MongoDB in python
Caveats:
1) This is an ugly hack that seems to work.
2) Field names must have at least one lower-case letter or no capitals
This will import 'M' (for MongoDB or Magic) and 'AND'
>>> from mongo_magic import *
Simple equality and inequality:
>>> M.foo == 'bar'
{'foo': 'bar'}
>>> M.foo.bar == 'baz'
{'foo.bar': 'baz'}
>>> M.foo.bar != 'qux'
{'foo.bar': {'$ne': 'qux'}}
Ranges:
>>> 0 < M.foo.bar < 100
{'foo.bar': {'$gt': 0, '$lt': 100}}
>>> 0 <= M.foo.bar <= 100
{'foo.bar': {'$lte': 100, '$gte': 0}}
Helpers that take varargs or iterables:
>>> M.foo.IN(1,2,3)
{'foo': {'$in': (1, 2, 3)}}
>>> M.foo.IN([1,2,3])
{'foo': {'$in': [1, 2, 3]}}
>>> M.foo.IN(set([1,2,3]))
{'foo': {'$in': [1, 2, 3]}}
>>> M.foo.NIN(1,2,3)
{'foo': {'$nin': (1, 2, 3)}}
>>> M.foo.ALL(1,2,3)
{'foo': {'$all': (1, 2, 3)}}
Single objects must be wrapped in a list:
>>> M.foo.IN('bar')
Traceback (most recent call last):
...
ValueError: $in doesn't make sense with single value
>>> M.foo.IN(['bar'])
{'foo': {'$in': ['bar']}}
$exists queries:
>>> M.foo.EXISTS()
{'foo': {'$exists': True}}
>>> M.foo.EXISTS(False)
{'foo': {'$exists': False}}
Regexes and startswith helper
>>> M.foo.RE("hello")
{'foo': {'$regex': 'hello'}}
>>> M.foo.RE("hello", 'i')
{'foo': {'$options': 'i', '$regex': 'hello'}}
>>> M.foo.STARTSWITH('world')
{'foo': {'$regex': '^world'}}
Combining requirements (must be for different fields):
>>> AND(M.a == 1, M.b > 10, M.c.IN('one', 'two'))
{'a': 1, 'c': {'$in': ('one', 'two')}, 'b': {'$gt': 10}}