@@ -511,6 +511,10 @@ def update(self, spec, document, upsert=False, manipulate=False,
511511 if not isinstance (upsert , bool ):
512512 raise TypeError ("upsert must be an instance of bool" )
513513
514+ client = self .database .connection
515+ # Need to connect to know the wire version, and may want to connect
516+ # before applying SON manipulators.
517+ client ._ensure_connected (True )
514518 if manipulate :
515519 document = self .__database ._fix_incoming (document , self )
516520
@@ -526,7 +530,6 @@ def update(self, spec, document, upsert=False, manipulate=False,
526530 if first .startswith ('$' ):
527531 check_keys = False
528532
529- client = self .database .connection
530533 if client .max_wire_version > 1 and safe :
531534 # Update command
532535 command = SON ([('update' , self .name )])
@@ -642,6 +645,9 @@ def remove(self, spec_or_id=None, safe=None, multi=True, **kwargs):
642645 safe , options = self ._get_write_mode (safe , ** kwargs )
643646
644647 client = self .database .connection
648+
649+ # Need to connect to know the wire version.
650+ client ._ensure_connected (True )
645651 if client .max_wire_version > 1 and safe :
646652 # Delete command
647653 command = SON ([('delete' , self .name )])
0 commit comments