Skip to content

Commit afa57fa

Browse files
author
Mike Dirolf
committed
add document_class parameter/property to Connection
1 parent 88c20df commit afa57fa

File tree

6 files changed

+58
-10
lines changed

6 files changed

+58
-10
lines changed

doc/api/pymongo/connection.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
.. automodule:: pymongo.connection
55
:synopsis: Tools for connecting to MongoDB
66

7-
.. autoclass:: pymongo.connection.Connection([host='localhost'[, port=27017[, pool_size=None[, auto_start_request=None[, timeout=None[, slave_okay=False[, network_timeout=None]]]]]]])
7+
.. autoclass:: pymongo.connection.Connection([host='localhost'[, port=27017[, pool_size=None[, auto_start_request=None[, timeout=None[, slave_okay=False[, network_timeout=None[, document_class=dict]]]]]]]])
88

99
.. automethod:: from_uri([uri='mongodb://localhost'])
1010
.. automethod:: paired(left[, right=('localhost', 27017)])
@@ -19,6 +19,7 @@
1919
.. autoattribute:: host
2020
.. autoattribute:: port
2121
.. autoattribute:: slave_okay
22+
.. autoattribute:: document_class
2223
.. automethod:: database_names
2324
.. automethod:: drop_database
2425
.. automethod:: copy_database(from_name, to_name[, from_host=None[, username=None[, password=None]]])

doc/api/pymongo/cursor.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
.. automodule:: pymongo.cursor
55
:synopsis: Tools for iterating over MongoDB query results
66

7-
.. autoclass:: pymongo.cursor.Cursor(collection, spec, fields, skip, limit, slave_okay, timeout, tailable[, snapshot=False[, sort=None[, max_scan=None]]])
7+
.. autoclass:: pymongo.cursor.Cursor
88
:members:
99

1010
.. describe:: c[index]

pymongo/collection.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,8 @@ def find(self, *args, **kwargs):
460460
- `max_scan` (optional): limit the number of documents
461461
examined when performing the query
462462
- `as_class` (optional): class to use for documents in the
463-
query result (default is :class:`dict`)
463+
query result (default is
464+
:attr:`~pymongo.connection.Connection.document_class`)
464465
465466
.. note:: The `max_scan` parameter requires server
466467
version **>= 1.5.1**

pymongo/connection.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ class Connection(object): # TODO support auth for pooling
108108

109109
def __init__(self, host=None, port=None, pool_size=None,
110110
auto_start_request=None, timeout=None, slave_okay=False,
111-
network_timeout=None, _connect=True):
111+
network_timeout=None, document_class=dict, _connect=True):
112112
"""Create a new connection to a single MongoDB instance at *host:port*.
113113
114114
The resultant connection object has connection-pooling built in. It
@@ -131,13 +131,17 @@ def __init__(self, host=None, port=None, pool_size=None,
131131
- `port` (optional): port number on which to connect
132132
- `pool_size` (optional): DEPRECATED
133133
- `auto_start_request` (optional): DEPRECATED
134-
- `slave_okay` (optional): is it okay to connect directly to and
135-
perform queries on a slave instance
134+
- `slave_okay` (optional): is it okay to connect directly to
135+
and perform queries on a slave instance
136136
- `timeout` (optional): DEPRECATED
137-
- `network_timeout` (optional): timeout (in seconds) to use for socket
138-
operations - default is no timeout
137+
- `network_timeout` (optional): timeout (in seconds) to use
138+
for socket operations - default is no timeout
139+
- `document_class` (optional): default class to use for
140+
documents returned from queries on this connection
139141
140142
.. seealso:: :meth:`end_request`
143+
.. versionadded:: 1.6+
144+
The `document_class` parameter.
141145
.. versionchanged:: 1.4
142146
DEPRECATED The `pool_size`, `auto_start_request`, and `timeout`
143147
parameters.
@@ -177,6 +181,7 @@ def __init__(self, host=None, port=None, pool_size=None,
177181
self.__pool = Pool(self.__connect)
178182

179183
self.__network_timeout = network_timeout
184+
self.__document_class = document_class
180185

181186
# cache of existing indexes used by ensure_index ops
182187
self.__index_cache = {}
@@ -404,6 +409,19 @@ def slave_okay(self):
404409
"""
405410
return self.__slave_okay
406411

412+
def get_document_class(self):
413+
return self.__document_class
414+
415+
def set_document_class(self, klass):
416+
self.__document_class = klass
417+
418+
document_class = property(get_document_class, set_document_class,
419+
doc="""Default class to use for documents
420+
returned from queries on this connection.
421+
422+
.. versionadded:: 1.6+
423+
""")
424+
407425
def __find_master(self):
408426
"""Create a new socket and use it to figure out who the master is.
409427

pymongo/cursor.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ def __init__(self, collection, spec=None, fields=None, skip=0, limit=0,
7575
fields = helpers._fields_list_to_dict(fields)
7676

7777
if as_class is None:
78-
# TODO add Connection level default for this case
79-
as_class = dict
78+
as_class = collection.database.connection.document_class
8079

8180
self.__collection = collection
8281
self.__spec = spec

test/test_connection.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
InvalidName,
3131
InvalidURI,
3232
OperationFailure)
33+
from pymongo.son import SON
3334
from test import version
3435

3536

@@ -347,6 +348,34 @@ def loop(pipe):
347348
except EOFError:
348349
pass
349350

351+
def test_document_class(self):
352+
c = Connection(self.host, self.port)
353+
db = c.pymongo_test
354+
db.test.insert({"x": 1})
355+
356+
self.assertEqual(dict, c.document_class)
357+
self.assert_(isinstance(db.test.find_one(), dict))
358+
self.failIf(isinstance(db.test.find_one(), SON))
359+
360+
c.document_class = SON
361+
362+
self.assertEqual(SON, c.document_class)
363+
self.assert_(isinstance(db.test.find_one(), SON))
364+
self.failIf(isinstance(db.test.find_one(as_class=dict), SON))
365+
366+
c = Connection(self.host, self.port, document_class=SON)
367+
db = c.pymongo_test
368+
369+
self.assertEqual(SON, c.document_class)
370+
self.assert_(isinstance(db.test.find_one(), SON))
371+
self.failIf(isinstance(db.test.find_one(as_class=dict), SON))
372+
373+
c.document_class = dict
374+
375+
self.assertEqual(dict, c.document_class)
376+
self.assert_(isinstance(db.test.find_one(), dict))
377+
self.failIf(isinstance(db.test.find_one(), SON))
378+
350379
# TODO come up with a different way to test `network_timeout`. This is just
351380
# too sketchy.
352381
#

0 commit comments

Comments
 (0)