3535from pymongo .errors import ConfigurationError , InvalidName , OperationFailure
3636from pymongo .helpers import _check_write_command_response
3737from pymongo .operations import _WriteOp , IndexModel
38+ from pymongo .read_concern import DEFAULT_READ_CONCERN
3839from pymongo .read_preferences import ReadPreference
3940from pymongo .results import (BulkWriteResult ,
4041 DeleteResult ,
@@ -71,7 +72,8 @@ class Collection(common.BaseObject):
7172 """
7273
7374 def __init__ (self , database , name , create = False , codec_options = None ,
74- read_preference = None , write_concern = None , ** kwargs ):
75+ read_preference = None , write_concern = None , read_concern = None ,
76+ ** kwargs ):
7577 """Get / create a Mongo collection.
7678
7779 Raises :class:`TypeError` if `name` is not an instance of
@@ -100,9 +102,15 @@ def __init__(self, database, name, create=False, codec_options=None,
100102 - `write_concern` (optional): An instance of
101103 :class:`~pymongo.write_concern.WriteConcern`. If ``None`` (the
102104 default) database.write_concern is used.
105+ - `read_concern` (optional): An instance of
106+ :class:`~pymongo.read_concern.ReadConcern`. If ``None`` (the
107+ default) database.read_concern is used.
103108 - `**kwargs` (optional): additional keyword arguments will
104109 be passed as options for the create collection command
105110
111+ .. versionchanged:: 3.2
112+ Added the read_concern option.
113+
106114 .. versionchanged:: 3.0
107115 Added the codec_options, read_preference, and write_concern options.
108116 Removed the uuid_subtype attribute.
@@ -128,7 +136,8 @@ def __init__(self, database, name, create=False, codec_options=None,
128136 super (Collection , self ).__init__ (
129137 codec_options or database .codec_options ,
130138 read_preference or database .read_preference ,
131- write_concern or database .write_concern )
139+ write_concern or database .write_concern ,
140+ read_concern or database .read_concern )
132141
133142 if not isinstance (name , string_type ):
134143 raise TypeError ("name must be an instance "
@@ -164,7 +173,8 @@ def _socket_for_writes(self):
164173
165174 def _command (self , sock_info , command , slave_ok = False ,
166175 read_preference = None ,
167- codec_options = None , check = True , allowable_errors = None ):
176+ codec_options = None , check = True , allowable_errors = None ,
177+ read_concern = DEFAULT_READ_CONCERN ):
168178 """Internal command helper.
169179
170180 :Parameters:
@@ -175,6 +185,8 @@ def _command(self, sock_info, command, slave_ok=False,
175185 :class:`~bson.codec_options.CodecOptions`.
176186 - `check`: raise OperationFailure if there are errors
177187 - `allowable_errors`: errors to ignore if `check` is True
188+ - `read_concern` (optional) - An instance of
189+ :class:`~pymongo.read_concern.ReadConcern`.
178190
179191 :Returns:
180192
@@ -188,7 +200,8 @@ def _command(self, sock_info, command, slave_ok=False,
188200 read_preference or self .read_preference ,
189201 codec_options or self .codec_options ,
190202 check ,
191- allowable_errors )
203+ allowable_errors ,
204+ read_concern = read_concern )
192205
193206 def __create (self , options ):
194207 """Sends a create command with the given options.
@@ -254,7 +267,8 @@ def database(self):
254267 return self .__database
255268
256269 def with_options (
257- self , codec_options = None , read_preference = None , write_concern = None ):
270+ self , codec_options = None , read_preference = None ,
271+ write_concern = None , read_concern = None ):
258272 """Get a clone of this collection changing the specified settings.
259273
260274 >>> coll1.read_preference
@@ -279,13 +293,18 @@ def with_options(
279293 :class:`~pymongo.write_concern.WriteConcern`. If ``None`` (the
280294 default) the :attr:`write_concern` of this :class:`Collection`
281295 is used.
296+ - `read_concern` (optional): An instance of
297+ :class:`~pymongo.read_concern.ReadConcern`. If ``None`` (the
298+ default) the :attr:`read_concern` of this :class:`Collection`
299+ is used.
282300 """
283301 return Collection (self .__database ,
284302 self .__name ,
285303 False ,
286304 codec_options or self .codec_options ,
287305 read_preference or self .read_preference ,
288- write_concern or self .write_concern )
306+ write_concern or self .write_concern ,
307+ read_concern or self .read_concern )
289308
290309 def initialize_unordered_bulk_op (self ):
291310 """Initialize an unordered batch of write operations.
@@ -1059,7 +1078,8 @@ def parallel_scan(self, num_cursors):
10591078 ('numCursors' , num_cursors )])
10601079
10611080 with self ._socket_for_reads () as (sock_info , slave_ok ):
1062- result = self ._command (sock_info , cmd , slave_ok )
1081+ result = self ._command (sock_info , cmd , slave_ok ,
1082+ read_concern = self .read_concern )
10631083
10641084 return [CommandCursor (self , cursor ['cursor' ], sock_info .address )
10651085 for cursor in result ['cursors' ]]
@@ -1068,7 +1088,8 @@ def _count(self, cmd):
10681088 """Internal count helper."""
10691089 with self ._socket_for_reads () as (sock_info , slave_ok ):
10701090 res = self ._command (sock_info , cmd , slave_ok ,
1071- allowable_errors = ["ns missing" ])
1091+ allowable_errors = ["ns missing" ],
1092+ read_concern = self .read_concern )
10721093 if res .get ("errmsg" , "" ) == "ns missing" :
10731094 return 0
10741095 return int (res ["n" ])
@@ -1522,7 +1543,8 @@ def aggregate(self, pipeline, **kwargs):
15221543
15231544 cmd .update (kwargs )
15241545
1525- result = self ._command (sock_info , cmd , slave_ok )
1546+ result = self ._command (sock_info , cmd , slave_ok ,
1547+ read_concern = self .read_concern )
15261548
15271549 if "cursor" in result :
15281550 cursor = result ["cursor" ]
@@ -1653,7 +1675,8 @@ def distinct(self, key, filter=None, **kwargs):
16531675 kwargs ["query" ] = filter
16541676 cmd .update (kwargs )
16551677 with self ._socket_for_reads () as (sock_info , slave_ok ):
1656- return self ._command (sock_info , cmd , slave_ok )["values" ]
1678+ return self ._command (sock_info , cmd , slave_ok ,
1679+ read_concern = self .read_concern )["values" ]
16571680
16581681 def map_reduce (self , map , reduce , out , full_response = False , ** kwargs ):
16591682 """Perform a map/reduce operation on this collection.
0 commit comments