@@ -1468,19 +1468,18 @@ def parallel_scan(self, num_cursors, session=None, **kwargs):
14681468 cmd .update (kwargs )
14691469
14701470 with self ._socket_for_reads () as (sock_info , slave_ok ):
1471- # Avoid auto-injecting a session.
1472- result = sock_info .command (
1473- self .__database .name ,
1474- cmd ,
1475- slave_ok ,
1476- self .read_preference ,
1477- self .codec_options ,
1478- read_concern = self .read_concern ,
1479- session = session )
1471+ result = self ._command (sock_info , cmd , slave_ok ,
1472+ read_concern = self .read_concern ,
1473+ session = session )
1474+
1475+ cursors = []
1476+ for cursor in result ['cursors' ]:
1477+ s = self .__database .client ._ensure_session (session )
1478+ cursors .append (CommandCursor (
1479+ self , cursor ['cursor' ], sock_info .address ,
1480+ session = s , explicit_session = session is not None ))
14801481
1481- return [CommandCursor (self , cursor ['cursor' ], sock_info .address ,
1482- session = session , explicit_session = True )
1483- for cursor in result ['cursors' ]]
1482+ return cursors
14841483
14851484 def _count (self , cmd , collation = None , session = None ):
14861485 """Internal count helper."""
@@ -2041,38 +2040,25 @@ def _aggregate(self, pipeline, cursor_class, first_batch_size, session,
20412040 cmd .update (kwargs )
20422041 # Apply this Collection's read concern if $out is not in the
20432042 # pipeline.
2044- if sock_info .max_wire_version >= 4 and 'readConcern' not in cmd :
2045- if dollar_out :
2046- # Avoid auto-injecting a session.
2047- result = sock_info .command (
2048- self .__database .name ,
2049- cmd ,
2050- slave_ok ,
2051- self .read_preference ,
2052- self .codec_options ,
2053- parse_write_concern_error = True ,
2054- collation = collation ,
2055- session = session )
2056- else :
2057- result = sock_info .command (
2058- self .__database .name ,
2059- cmd ,
2060- slave_ok ,
2061- ReadPreference .PRIMARY ,
2062- self .codec_options ,
2063- read_concern = self .read_concern ,
2064- collation = collation ,
2065- session = session )
2043+ if (sock_info .max_wire_version >= 4
2044+ and 'readConcern' not in cmd
2045+ and not dollar_out ):
2046+ read_concern = self .read_concern
20662047 else :
2067- result = sock_info .command (
2068- self .__database .name ,
2069- cmd ,
2070- slave_ok ,
2071- self .read_preference ,
2072- self .codec_options ,
2073- parse_write_concern_error = dollar_out ,
2074- collation = collation ,
2075- session = session )
2048+ read_concern = DEFAULT_READ_CONCERN
2049+
2050+ # Avoid auto-injecting a session: aggregate() passes a session,
2051+ # aggregate_raw_batches() passes none.
2052+ result = sock_info .command (
2053+ self .__database .name ,
2054+ cmd ,
2055+ slave_ok ,
2056+ self .read_preference ,
2057+ self .codec_options ,
2058+ parse_write_concern_error = dollar_out ,
2059+ read_concern = read_concern ,
2060+ collation = collation ,
2061+ session = session )
20762062
20772063 if "cursor" in result :
20782064 cursor = result ["cursor" ]
@@ -2202,7 +2188,8 @@ def aggregate_raw_batches(self, pipeline, **kwargs):
22022188 None , False , ** kwargs )
22032189
22042190 def watch (self , pipeline = None , full_document = 'default' , resume_after = None ,
2205- max_await_time_ms = None , batch_size = None , collation = None ):
2191+ max_await_time_ms = None , batch_size = None , collation = None ,
2192+ session = None ):
22062193 """Watch changes on this collection.
22072194
22082195 Performs an aggregation with an implicit initial
@@ -2266,6 +2253,8 @@ def watch(self, pipeline=None, full_document='default', resume_after=None,
22662253 per batch.
22672254 - `collation` (optional): The :class:`~pymongo.collation.Collation`
22682255 to use for the aggregation.
2256+ - `session` (optional): a
2257+ :class:`~pymongo.client_session.ClientSession`.
22692258
22702259 :Returns:
22712260 A :class:`~pymongo.change_stream.ChangeStream` cursor.
@@ -2287,7 +2276,7 @@ def watch(self, pipeline=None, full_document='default', resume_after=None,
22872276 common .validate_string_or_none ('full_document' , full_document )
22882277
22892278 return ChangeStream (self , pipeline , full_document , resume_after ,
2290- max_await_time_ms , batch_size , collation )
2279+ max_await_time_ms , batch_size , collation , session )
22912280
22922281 def group (self , key , condition , initial , reduce , finalize = None , ** kwargs ):
22932282 """Perform a query similar to an SQL *group by* operation.
0 commit comments