@@ -8,8 +8,8 @@ database. Python developers can think of MongoDB as a persistent, searchable
88repository of Python dictionaries (and, in fact, this is how `PyMongo
99<http://api.mongodb.org/python/current/> `_ represents MongoDB documents).
1010
11- Flask-PyMongo bridges Flask and PyMongo, so that you can use Flask's normal
12- mechanisms to configure and connect to MongoDB .
11+ Flask-PyMongo bridges Flask and PyMongo and provides some convenience
12+ helpers .
1313
1414
1515Quickstart
@@ -29,12 +29,12 @@ Next, add a :class:`~flask_pymongo.PyMongo` to your code:
2929 from flask_pymongo import PyMongo
3030
3131 app = Flask(__name__ )
32+ app.config[" MONGO_URI" ] = " mongodb://localhost:27017/myDatabase"
3233 mongo = PyMongo(app)
3334
3435:class: `~flask_pymongo.PyMongo ` connects to the MongoDB server running on
35- port 27017 on localhost, and assumes a default database name of ``app.name ``
36- (i.e. whatever name you pass to :class: `~flask.Flask `). This database is
37- exposed as the :attr: `~flask_pymongo.PyMongo.db ` attribute.
36+ port 27017 on localhost, to the database named ``myDatabase ``. This database
37+ is exposed as the :attr: `~flask_pymongo.PyMongo.db ` attribute.
3838
3939You can use :attr: `~flask_pymongo.PyMongo.db ` directly in views:
4040
@@ -59,7 +59,8 @@ versions.
5959Flask-PyMongo is tested against `supported versions
6060<https://www.mongodb.com/support-policy> `_ of the MongoDB, and Python 2.7
6161and 3.4+. For the exact list of version combinations that are tested and
62- known to be compatible, see the `envlist ` in `tox.ini `.
62+ known to be compatible, see the `envlist ` in `tox.ini
63+ <https://github.com/dcrosta/flask-pymongo/blob/master/tox.ini> `_.
6364
6465
6566Helpers
@@ -78,134 +79,38 @@ Flask-PyMongo provides helpers for some common tasks:
7879Configuration
7980-------------
8081
81- :class: `~flask_pymongo.PyMongo ` understands the following configuration
82- directives:
83-
84- ===================================== =========================================
85- ``MONGO_URI `` A `MongoDB URI
86- <http://www.mongodb.org/display/DOCS/Connections#Connections-StandardConnectionStringFormat> `_
87- which is used in preference of the other
88- configuration variables.
89- ``MONGO_HOST `` The host name or IP address of your
90- MongoDB server. Default: "localhost".
91- ``MONGO_PORT `` The port number of your MongoDB server.
92- Default: 27017.
93- ``MONGO_AUTO_START_REQUEST `` Set to ``False `` to disable PyMongo 2.2's
94- "auto start request" behavior (see
95- :class: `~pymongo.mongo_client.MongoClient `).
96- Default: ``True ``.
97- ``MONGO_MAX_POOL_SIZE `` (optional): The maximum number of idle
98- connections maintained in the PyMongo
99- connection pool. Default: PyMongo
100- default.
101- ``MONGO_SOCKET_TIMEOUT_MS `` (optional): (integer) How long (in
102- milliseconds) a send or receive on a
103- socket can take before timing out.
104- Default: PyMongo default.
105- ``MONGO_CONNECT_TIMEOUT_MS `` (optional): (integer) How long (in
106- milliseconds) a connection can take to be
107- opened before timing out.
108- Default: PyMongo default.
109- ``MONGO_SERVER_SELECTION_TIMEOUT_MS `` (optional) Controls how long (in
110- milliseconds) the driver will wait to
111- find an available, appropriate server to
112- carry out a database operation; while it
113- is waiting, multiple server monitoring
114- operations may be carried out, each
115- controlled by ``connectTimeoutMS ``.
116- Default: PyMongo default.
117- ``MONGO_DBNAME `` The database name to make available as
118- the ``db `` attribute.
119- Default: ``app.name ``.
120- ``MONGO_USERNAME `` The user name for authentication.
121- Default: ``None ``
122- ``MONGO_PASSWORD `` The password for authentication.
123- Default: ``None ``
124- ``MONGO_AUTH_SOURCE `` The database to authenticate against.
125- Default: ``None ``
126- ``MONGO_AUTH_MECHANISM `` The mechanism to authenticate with.
127- Default: pymongo <3.x ``MONGODB-CR ``
128- else ``SCRAM-SHA-1 ``
129- ``MONGO_REPLICA_SET `` The name of a replica set to connect to;
130- this must match the internal name of the
131- replica set (as determined by the
132- `isMaster
133- <http://www.mongodb.org/display/DOCS/Replica+Set+Commands#ReplicaSetCommands-isMaster> `_
134- command). Default: ``None ``.
135- ``MONGO_READ_PREFERENCE `` Determines how read queries are routed to
136- the replica set members. Must be one of
137- the constants defined on
138- :class: `pymongo.read_preferences.ReadPreference `
139- or the string names thereof.
140- ``MONGO_DOCUMENT_CLASS `` This tells pymongo to return custom
141- objects instead of dicts, for example
142- ``bson.son.SON ``. Default: ``dict ``
143- ``MONGO_CONNECT `` (optional): If ``True `` (the default),
144- let the MongoClient immediately begin
145- connecting to MongoDB in the background.
146- Otherwise connect on the first operation.
147- This has to be set to ``False `` if
148- multiprocessing is desired; see
149- `Using PyMongo with Multiprocessing
150- <https://api.mongodb.org/python/current/faq.html#multiprocessing> `_.
151- ===================================== =========================================
152-
153- When :class: `~flask_pymongo.PyMongo ` or
154- :meth: `~flask_pymongo.PyMongo.init_app ` are invoked with only one argument
155- (the :class: `~flask.Flask ` instance), a configuration value prefix of
156- ``MONGO `` is assumed; this can be overridden with the `config_prefix `
157- argument.
158-
159- This technique can be used to connect to multiple databases or database
160- servers:
82+ You can configure Flask-PyMongo either by passing a `MongoDB URI
83+ <https://docs.mongodb.com/manual/reference/connection-string/> `_ to the
84+ :class: `~flask_pymongo.PyMongo ` constructor, or assigning it to the
85+ ``MONGO_URI `` `Flask configuration variable
86+ <http://flask.pocoo.org/docs/1.0/config/> `_
87+
88+ You may also pass additional keyword arguments to the ``PyMongo ``
89+ constructor. These are passed directly through to the underlying
90+ :class: `~pymongo.mongo_client.MongoClient ` object.
91+
92+ You can create multiple ``PyMongo `` instances, to connect to multiple
93+ databases or database servers:
16194
16295.. code-block :: python
16396
16497 app = Flask(__name__ )
16598
16699 # connect to MongoDB with the defaults
167- mongo1 = PyMongo(app)
100+ mongo1 = PyMongo(app, uri = " mongodb://localhost:27017/databaseOne " )
168101
169102 # connect to another MongoDB database on the same host
170- app.config[" MONGO2_DBNAME" ] = " dbname_two"
171- mongo2 = PyMongo(app, config_prefix = " MONGO2" )
103+ mongo2 = PyMongo(app, uri = " mongodb://localhost:27017/databaseTwo" )
172104
173105 # connect to another MongoDB server altogether
174- app.config[" MONGO3_HOST" ] = " another.host.example.com"
175- app.config[" MONGO3_PORT" ] = 27017
176- app.config[" MONGO3_DBNAME" ] = " dbname_three"
177- mongo3 = PyMongo(app, config_prefix = " MONGO3" )
178-
179- Some auto-configured settings that you should be aware of are:
180-
181- ``tz_aware ``:
182- Flask-PyMongo always uses timezone-aware :class: `~datetime.datetime `
183- objects. That is, it sets the ``tz_aware `` parameter to ``True `` when
184- creating a connection. The timezone of :class: `~datetime.datetime `
185- objects returned from MongoDB will always be UTC.
186-
187- ``safe ``:
188- Flask-PyMongo sets "safe" mode by default, which causes
189- :meth: `~pymongo.collection.Collection.save `,
190- :meth: `~pymongo.collection.Collection.insert `,
191- :meth: `~pymongo.collection.Collection.update `, and
192- :meth: `~pymongo.collection.Collection.remove ` to wait for acknowledgement
193- from the server before returning. You may override this on a per-call
194- basis by passing the keyword argument ``safe=False `` to any of the
195- effected methods.
106+ mongo3 = PyMongo(app, uri = " mongodb://another.host:27017/databaseThree" )
107+
108+ Each instance is independent of the others and shares no state.
196109
197110
198111API
199112===
200113
201- Constants
202- ---------
203-
204- .. autodata :: flask_pymongo.ASCENDING
205-
206- .. autodata :: flask_pymongo.DESCENDING
207-
208-
209114Classes
210115-------
211116
@@ -224,26 +129,26 @@ Classes
224129 named a database, and ``None `` otherwise.
225130
226131
227- .. autoclass :: flask_pymongo.wrappers.Collection
228- :members:
229-
230-
231132Wrappers
232133--------
233134
234- These classes exist solely in order to make expressions such as
235- ``mongo.db.foo.bar `` evaluate to a
236- :class: `~flask_pymongo.wrappers.Collection ` instance instead of
237- a :class: `pymongo.collection.Collection ` instance. They are documented here
238- solely for completeness.
135+ Flask-PyMongo wraps PyMongo's :class: `~pymongo.mongo_client.MongoClient `,
136+ :class: `~pymongo.database.Database `, and
137+ :class: `~pymongo.collection.Collection ` classes, and overrides their
138+ attribute and item accessors. Wrapping the PyMongo classes in this way lets
139+ Flask-PyMongo add methods to ``Collection `` while allowing user code to use
140+ MongoDB-style dotted expressions.
239141
240- .. autoclass :: flask_pymongo.wrappers.MongoClient
241- :members:
142+ .. code-block :: python
242143
243- .. autoclass :: flask_pymongo.wrappers.MongoReplicaSetClient
244- :members:
144+ >> > type (mongo.cx)
145+ < type ' flask_pymongo.wrappers.MongoClient' >
146+ >> > type (mongo.db)
147+ < type ' flask_pymongo.wrappers.Database' >
148+ >> > type (mongo.db.some_collection)
149+ < type ' flask_pymongo.wrappers.Collection' >
245150
246- .. autoclass :: flask_pymongo.wrappers.Database
151+ .. autoclass :: flask_pymongo.wrappers.Collection(...)
247152 :members:
248153
249154
@@ -254,7 +159,12 @@ Changes:
254159
255160- 2.0.0: *unreleased *
256161
257- - Clarify version support of Python, Flask, PyMongo, and MongoDB
162+ **This release is not compatible with Flask-PyMongo 0.5.x or any earlier
163+ version. ** You can see an explanation of the reasoning and changes in
164+ `issue #110 <https://github.com/dcrosta/flask-pymongo/issues/110 >`_.
165+
166+ - Only support configuration via URI.
167+ - Clarify version support of Python, Flask, PyMongo, and MongoDB.
258168
259169- 0.5.2: May 19, 2018
260170
0 commit comments