Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/studio/static/doc/flask-docs/_sources/patterns/mongokit.txt
blob: a9c4eef532b98727b4c935123c4ef7533603f279 (plain)
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
.. mongokit-pattern:

MongoKit in Flask
=================

Using a document database rather than a full DBMS gets more common these days.
This pattern shows how to use MongoKit, a document mapper library, to
integrate with MongoDB.

This pattern requires a running MongoDB server and the MongoKit library
installed.

There are two very common ways to use MongoKit.  I will outline each of them
here:


Declarative
-----------

The default behaviour of MongoKit is the declarative one that is based on
common ideas from Django or the SQLAlchemy declarative extension.

Here an example `app.py` module for your application::

    from flask import Flask
    from mongokit import Connection, Document

    # configuration
    MONGODB_HOST = 'localhost'
    MONGODB_PORT = 27017

    # create the little application object
    app = Flask(__name__)
    app.config.from_object(__name__)

    # connect to the database
    connection = Connection(app.config['MONGODB_HOST'],
                            app.config['MONGODB_PORT'])


To define your models, just subclass the `Document` class that is imported
from MongoKit.  If you've seen the SQLAlchemy pattern you may wonder why we do
not have a session and even do not define a `init_db` function here.  On the
one hand, MongoKit does not have something like a session.  This sometimes
makes it more to type but also makes it blazingly fast.  On the other hand,
MongoDB is schemaless.  This means you can modify the data structure from one
insert query to the next without any problem.  MongoKit is just schemaless
too, but implements some validation to ensure data integrity.

Here is an example document (put this also into `app.py`, e.g.)::

    def max_length(length):
        def validate(value):
            if len(value) <= length:
                return True
            raise Exception('%s must be at most %s characters long' % length)
        return validate

    class User(Document):
        structure = {
            'name': unicode,
            'email': unicode,
        }
        validators = {
            'name': max_length(50),
            'email': max_length(120)
        }
        use_dot_notation = True
        def __repr__(self):
            return '<User %r>' % (self.name)

    # register the User document with our current connection
    connection.register([User])


This example shows you how to define your schema (named structure), a
validator for the maximum character length and uses a special MongoKit feature
called `use_dot_notation`.  Per default MongoKit behaves like a python
dictionary but with `use_dot_notation` set to `True` you can use your
documents like you use models in nearly any other ORM by using dots to
separate between attributes.

You can insert entries into the database like this:

>>> from yourapplication.database import connection
>>> from yourapplication.models import User
>>> collection = connection['test'].users
>>> user = collection.User()
>>> user['name'] = u'admin'
>>> user['email'] = u'admin@localhost'
>>> user.save()

Note that MongoKit is kinda strict with used column types, you must not use a
common `str` type for either `name` or `email` but unicode.

Querying is simple as well:

>>> list(collection.User.find())
[<User u'admin'>]
>>> collection.User.find_one({'name': u'admin'})
<User u'admin'>

.. _MongoKit: http://bytebucket.org/namlook/mongokit/


PyMongo Compatibility Layer
---------------------------

If you just want to use PyMongo, you can do that with MongoKit as well.  You
may use this process if you need the best performance to get.  Note that this
example does not show how to couple it with Flask, see the above MongoKit code
for examples::

    from MongoKit import Connection

    connection = Connection()

To insert data you can use the `insert` method.  We have to get a
collection first, this is somewhat the same as a table in the SQL world.

>>> collection = connection['test'].users
>>> user = {'name': u'admin', 'email': u'admin@localhost'}
>>> collection.insert(user)

print list(collection.find())
print collection.find_one({'name': u'admin'})

MongoKit will automatically commit for us.

To query your database, you use the collection directly:

>>> list(collection.find())
[{u'_id': ObjectId('4c271729e13823182f000000'), u'name': u'admin', u'email': u'admin@localhost'}]
>>> collection.find_one({'name': u'admin'})
{u'_id': ObjectId('4c271729e13823182f000000'), u'name': u'admin', u'email': u'admin@localhost'}

These results are also dict-like objects:

>>> r = collection.find_one({'name': u'admin'})
>>> r['email']
u'admin@localhost'

For more information about MongoKit, head over to the
`website <http://bytebucket.org/namlook/mongokit/>`_.