@@ -511,6 +511,10 @@ def update(self, spec, document, upsert=False, manipulate=False,
511
511
if not isinstance (upsert , bool ):
512
512
raise TypeError ("upsert must be an instance of bool" )
513
513
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 )
514
518
if manipulate :
515
519
document = self .__database ._fix_incoming (document , self )
516
520
@@ -526,7 +530,6 @@ def update(self, spec, document, upsert=False, manipulate=False,
526
530
if first .startswith ('$' ):
527
531
check_keys = False
528
532
529
- client = self .database .connection
530
533
if client .max_wire_version > 1 and safe :
531
534
# Update command
532
535
command = SON ([('update' , self .name )])
@@ -642,6 +645,9 @@ def remove(self, spec_or_id=None, safe=None, multi=True, **kwargs):
642
645
safe , options = self ._get_write_mode (safe , ** kwargs )
643
646
644
647
client = self .database .connection
648
+
649
+ # Need to connect to know the wire version.
650
+ client ._ensure_connected (True )
645
651
if client .max_wire_version > 1 and safe :
646
652
# Delete command
647
653
command = SON ([('delete' , self .name )])
0 commit comments