This repository has been archived by the owner on Jul 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 132
/
document.txt
183 lines (126 loc) · 4.73 KB
/
document.txt
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
=========
Documents
=========
Documents are the basic building blocks of MongoKit. They define the schema
of each document and how that document should be accessed
Document Class
--------------
::
from mongokit import Document, Connection
class MyDocument(Document):
structure = {
...
}
required_fields = [
...
]
default_values = {
...
}
You can read more about the `structure`_ attribute, and the ``required_fields`` and ``default_values`` `descriptors`_. They are the primary definition of a document. MongoKit also supports
`handling i18n`_, `indexes`_, and `migration`_.
.. _`structure`: structure.html
.. _`descriptors`: descriptors.html
.. _`handling i18n`: i18n.html
.. _`indexes`: indexes.html
.. _`migration`: migration.html
Registering
-----------
Once a document has been defined, it must be registered with a Connection::
connection = Connection()
connection.register([MyDocument])
Optionally, the register method can be used as a decorator::
@connection.register
class MyDocument(Document):
structure = {...}
Database and Collection
-----------------------
To use a Document, you must call it from a collection. In pymongo's syntax, you would use
``connection.<database>.<collection>`` to access the collection. Once you have
the collection, you can create a new document::
>>> connection.database.collection.MyDocument()
{... new Document's default values ...}
As a short cut, you can define the database and collection names in the
Document definition::
@connection.register
class MyDocument(Document):
__collection__ = 'collection_name'
__database__ = 'database_name'
structure = {...}
Now, we can have access to our document directly from the connection::
>>> connection.MyDocument()
{... new Document's default values ...}
Note that if you want to specify the ``__database__``, you should also specify the
``__collection__`` attribute.
It is also possible to access the Document from the database:
>>> connection.database.MyDocument() # this will use __collection__ as collection name
This matches the typical pattern of creating and passing around a ``db`` object:
>>> connection = Connection()
>>> db = connection[MONGODB_DATABASE_NAME]
>>> db.MyDocument()
Changing Collection Dynamically
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
You might need to specify a different db or collection dynamically. For instance,
say you want to store a User by database.
>>> class User(Document):
... structure = {'login':unicode, 'screen_name':unicode}
>>> con.register([User])
Like pymongo, MongoKit allow you to change those parameters on the fly :
>>> user_name = 'namlook'
>>> user_collection = connection[user_name].profile #returns a reference to the database 'namlook' in the collection 'profile'.
Now, we can query the database by passing our new collection :
>>> profiles = user_collection.User.find()
>>> user = user_collection.User()
>>> user['login'] = '[email protected]'
>>> user['screen_name'] = 'Namlook'
Calling ``user.save()`` will save the object into the database ``namlook``
in the collection ``profile``.
Dot Notation
------------
If you want to use the dot notation (ala json), you must set the
``use_dot_notation`` attribute to True::
class TestDotNotation(Document):
use_dot_notation = True
structure = {
'foo':{
'bar': basestring
}
}
>>> connection.register([TestDotNotation])
>>> doc = connection.database.TestDotNotation()
>>> doc.foo.bar = 'blah'
>>> doc
{'foo': {'bar': 'blah'}}
Note that if an attribute is not in structure, the value will be added as attribute :
>>> doc.arf = 3 # arf is not in structure
>>> doc
{'foo': {'bar': u'bla'}}
If you want to be warned when a value is set as attribute, you can set the `dot_notation_warning` attribute as True.
Polymorphism
------------
In the following example, we have two objects, A and B, which inherit from Root.
And we want to build an object C from A and B. Let's build Root, A and B
first::
from mongokit import *
class Root(Document):
structure = {
'root': int
}
required_fields = ['root']
class A(Root):
structure = {
'a_field': basestring,
}
required_fields = ['a_field']
class B(Root):
structure = {
'b_field': basestring,
}
Polymorphisms just work as expected::
class C(A,B):
structure = {'c_field': float}
>>> c = C()
>>> c == {'b_field': None, 'root': None, 'c_field': None, 'a_field': None}
True
>>> C.required_fields
['root', 'a_field']