2727from pymongo import ASCENDING
2828from pymongo .collection import Collection
2929from pymongo .cursor import Cursor
30- from pymongo .errors import DuplicateKeyError , InvalidOperation
30+ from pymongo .errors import ConfigurationError , DuplicateKeyError
3131from pymongo .read_preferences import ReadPreference
3232
3333try :
@@ -62,9 +62,8 @@ def getter(self):
6262
6363 def setter (self , value ):
6464 if self ._closed :
65- self ._coll .files .update ({"_id" : self ._file ["_id" ]},
66- {"$set" : {field_name : value }},
67- ** self ._coll ._get_wc_override ())
65+ self ._coll .files .update_one ({"_id" : self ._file ["_id" ]},
66+ {"$set" : {field_name : value }})
6867 self ._file [field_name ] = value
6968
7069 if read_only :
@@ -135,12 +134,18 @@ def __init__(self, root_collection, **kwargs):
135134 - `**kwargs` (optional): file level options (see above)
136135
137136 .. versionchanged:: 3.0
138- w=0 writes to GridFS are now prohibited.
137+ `root_collection` must use an acknowledged
138+ :attr:`~pymongo.collection.Collection.write_concern`
139139 """
140140 if not isinstance (root_collection , Collection ):
141141 raise TypeError ("root_collection must be an "
142142 "instance of Collection" )
143143
144+ # With w=0, 'filemd5' might run before the final chunks are written.
145+ if not root_collection .write_concern .acknowledged :
146+ raise ConfigurationError ('root_collection must use '
147+ 'acknowledged write_concern' )
148+
144149 # Handle alternative naming
145150 if "content_type" in kwargs :
146151 kwargs ["contentType" ] = kwargs .pop ("content_type" )
@@ -204,9 +209,8 @@ def __setattr__(self, name, value):
204209 # them now.
205210 self ._file [name ] = value
206211 if self ._closed :
207- self ._coll .files .update ({"_id" : self ._file ["_id" ]},
208- {"$set" : {name : value }},
209- ** self ._coll ._get_wc_override ())
212+ self ._coll .files .update_one ({"_id" : self ._file ["_id" ]},
213+ {"$set" : {name : value }})
210214
211215 def __flush_data (self , data ):
212216 """Flush `data` to a chunk.
@@ -224,7 +228,7 @@ def __flush_data(self, data):
224228 "data" : Binary (data )}
225229
226230 try :
227- self ._chunks .insert (chunk )
231+ self ._chunks .insert_one (chunk )
228232 except DuplicateKeyError :
229233 self ._raise_file_exists (self ._file ['_id' ])
230234 self ._chunk_number += 1
@@ -252,8 +256,7 @@ def __flush(self):
252256 self ._file ["length" ] = self ._position
253257 self ._file ["uploadDate" ] = datetime .datetime .utcnow ()
254258
255- return self ._coll .files .insert (self ._file ,
256- ** self ._coll ._get_wc_override ())
259+ return self ._coll .files .insert_one (self ._file )
257260 except DuplicateKeyError :
258261 self ._raise_file_exists (self ._id )
259262
@@ -296,10 +299,6 @@ def write(self, data):
296299 if self ._closed :
297300 raise ValueError ("cannot write to a closed file" )
298301
299- # With w=0, 'filemd5' might run before the final chunks are written.
300- if not self ._coll .write_concern .acknowledged :
301- raise InvalidOperation ('Cannot write file to GridFS with w=0' )
302-
303302 try :
304303 # file-like
305304 read = data .read
0 commit comments