-
Notifications
You must be signed in to change notification settings - Fork 5
Basics
First of all, MongoLite relied heavily on pymongo. As such, all pymongo API is exposed. If you don't find what you want in the Mongokit document, please take a look at pymongo's one. All the pymongo API is exposed via connection, database and collection so Connection, Database and Collection are wrapper around pymongo objects.
Let's create our first object. First of all we need to instanciate a connection:
from mongolite import Connection
connection = Connection()
Now we can create a document:
from mongolite import Document
@connection.register
class BlogPost(Document):
__database__ = "tutorial"
__collection__ = "blogposts"
skeleton = {
'title':unicode,
'body':unicode,
'author':unicode,
'date_creation':datetime.datetime,
}
optional = {
'tags': [unicode],
'rank':int,
}
default_values = {'rank':0, 'date_creation':datetime.datetime.utcnow}
Let's explain step by step. Our document is register to the connection via the decorator @connection.register
.
You can specify the database and the collection related to the document via the attribute __database__
and __collection__
.
Note that if you want to specify the __database__
you should also specify the __collection__
attribute.
Now let's see the document structure. By convention, all fields which are required should be declared into the skeleton part. Optional fields are set into optional
.
This is handy for two things :
- first, it is a good documentation to see what fields are always in the database and thoses who might not be present.
- second, when a document is instanciate, a skeleton is generated based on fields present into the
skeleton
.
In summary, while fields in skeleton
should be present in the document, fields in optional
attribut are not generated by default. It aims to be for documentation only...
All document are registered against a connection so to access to a document just use the connection :
>>> blogpost = connection.BlogPost()
You can to use another database or collection than thoses set into __database__
and __collection__
:
>>> blogpost = connection.other_database.other_collection.BlogPost()
It is also possible to access the documents from the database::
>>> blogpost = connection.other_database.BlogPost() # this will use __collection__ as collection name
The blogpost
object is a dict instance which have a skeleton generated:
>>> blogpost
{'body': None, 'title': None, 'date_creation': datetime.datetime(...), 'author': None}
Note that only fields present into skeleton
are set. date_creation
has a default value specified into default_values
.
You can do it like this:
connection.register([Document1, Document2, Document3])
If the database or the collection changes dynamically, then don't set the __database__
or __collection__
attribute. The following example assumes that the database is dynamic and __collection__
is set:
>>> doc = connection[my_dynamic_database].MyDocument()
See next how to query document