@@ -284,8 +284,8 @@ async def delete(self, file_id: Any, session: Optional[AsyncClientSession] = Non
284284 ``delete`` no longer ensures indexes.
285285 """
286286 _disallow_transactions (session )
287- await self ._files .delete_one ({"_id" : file_id }, session = session )
288- await self ._chunks .delete_many ({"files_id" : file_id }, session = session )
287+ await self ._files .delete_one ({"_id" : { "$eq" : file_id } }, session = session )
288+ await self ._chunks .delete_many ({"files_id" : { "$eq" : file_id } }, session = session )
289289
290290 async def list (self , session : Optional [AsyncClientSession ] = None ) -> list [str ]:
291291 """List the names of all files stored in this instance of
@@ -341,7 +341,7 @@ async def find_one(
341341 Added ``session`` parameter.
342342 """
343343 if filter is not None and not isinstance (filter , abc .Mapping ):
344- filter = {"_id" : filter }
344+ filter = {"_id" : { "$eq" : filter } }
345345
346346 _disallow_transactions (session )
347347 async for f in self .find (filter , * args , session = session , ** kwargs ):
@@ -455,6 +455,8 @@ async def exists(
455455 if kwargs :
456456 f = await self ._files .find_one (kwargs , ["_id" ], session = session )
457457 else :
458+ if document_or_id is not None and not isinstance (document_or_id , abc .Mapping ):
459+ document_or_id = {"_id" : {"$eq" : document_or_id }}
458460 f = await self ._files .find_one (document_or_id , ["_id" ], session = session )
459461
460462 return f is not None
@@ -831,8 +833,8 @@ async def delete(self, file_id: Any, session: Optional[AsyncClientSession] = Non
831833 Added ``session`` parameter.
832834 """
833835 _disallow_transactions (session )
834- res = await self ._files .delete_one ({"_id" : file_id }, session = session )
835- await self ._chunks .delete_many ({"files_id" : file_id }, session = session )
836+ res = await self ._files .delete_one ({"_id" : { "$eq" : file_id } }, session = session )
837+ await self ._chunks .delete_many ({"files_id" : { "$eq" : file_id } }, session = session )
836838 if not res .deleted_count :
837839 raise NoFile (f"no file could be deleted because none matched { file_id } " )
838840
@@ -1044,7 +1046,7 @@ async def rename(
10441046 """
10451047 _disallow_transactions (session )
10461048 result = await self ._files .update_one (
1047- {"_id" : file_id }, {"$set" : {"filename" : new_filename }}, session = session
1049+ {"_id" : { "$eq" : file_id } }, {"$set" : {"filename" : new_filename }}, session = session
10481050 )
10491051 if not result .matched_count :
10501052 raise NoFile (
@@ -1198,8 +1200,12 @@ async def _ensure_indexes(self) -> None:
11981200
11991201 async def abort (self ) -> None :
12001202 """Remove all chunks/files that may have been uploaded and close."""
1201- await self ._coll .chunks .delete_many ({"files_id" : self ._file ["_id" ]}, session = self ._session )
1202- await self ._coll .files .delete_one ({"_id" : self ._file ["_id" ]}, session = self ._session )
1203+ await self ._coll .chunks .delete_many (
1204+ {"files_id" : {"$eq" : self ._file ["_id" ]}}, session = self ._session
1205+ )
1206+ await self ._coll .files .delete_one (
1207+ {"_id" : {"$eq" : self ._file ["_id" ]}}, session = self ._session
1208+ )
12031209 object .__setattr__ (self , "_closed" , True )
12041210
12051211 @property
@@ -1248,7 +1254,9 @@ def __setattr__(self, name: str, value: Any) -> None:
12481254 self ._file [name ] = value
12491255 if self ._closed :
12501256 if _IS_SYNC :
1251- self ._coll .files .update_one ({"_id" : self ._file ["_id" ]}, {"$set" : {name : value }})
1257+ self ._coll .files .update_one (
1258+ {"_id" : {"$eq" : self ._file ["_id" ]}}, {"$set" : {name : value }}
1259+ )
12521260 else :
12531261 raise AttributeError (
12541262 "AsyncGridIn does not support __setattr__ after being closed(). Set the attribute before closing the file or use AsyncGridIn.set() instead"
@@ -1257,7 +1265,9 @@ def __setattr__(self, name: str, value: Any) -> None:
12571265 async def set (self , name : str , value : Any ) -> None :
12581266 self ._file [name ] = value
12591267 if self ._closed :
1260- await self ._coll .files .update_one ({"_id" : self ._file ["_id" ]}, {"$set" : {name : value }})
1268+ await self ._coll .files .update_one (
1269+ {"_id" : {"$eq" : self ._file ["_id" ]}}, {"$set" : {name : value }}
1270+ )
12611271
12621272 async def _flush_data (self , data : Any , force : bool = False ) -> None :
12631273 """Flush `data` to a chunk."""
@@ -1582,7 +1592,9 @@ async def readlines(self, size: int = -1) -> list[bytes]:
15821592 async def open (self ) -> None :
15831593 if not self ._file :
15841594 _disallow_transactions (self ._session )
1585- self ._file = await self ._files .find_one ({"_id" : self ._file_id }, session = self ._session )
1595+ self ._file = await self ._files .find_one (
1596+ {"_id" : {"$eq" : self ._file_id }}, session = self ._session
1597+ )
15861598 if not self ._file :
15871599 raise NoFile (
15881600 f"no file in gridfs collection { self ._files !r} with _id { self ._file_id !r} "
@@ -1853,7 +1865,7 @@ def __aiter__(self) -> _AsyncGridOutChunkIterator:
18531865 return self
18541866
18551867 def _create_cursor (self ) -> None :
1856- filter = {"files_id" : self ._id }
1868+ filter = {"files_id" : { "$eq" : self ._id } }
18571869 if self ._next_chunk > 0 :
18581870 filter ["n" ] = {"$gte" : self ._next_chunk }
18591871 _disallow_transactions (self ._session )
0 commit comments