Get a file from GridFS by ``"filename"`` or metadata fields. Returns a version of the file in GridFS whose filename matches `filename` and whose metadata fields match the supplied keyword arguments, as an instance of :class:`~gridfs.grid_file.GridOut`. Version numbe
(
self,
filename: Optional[str] = None,
version: Optional[int] = -1,
session: Optional[AsyncClientSession] = None,
**kwargs: Any,
)
| 176 | return gout |
| 177 | |
| 178 | async def get_version( |
| 179 | self, |
| 180 | filename: Optional[str] = None, |
| 181 | version: Optional[int] = -1, |
| 182 | session: Optional[AsyncClientSession] = None, |
| 183 | **kwargs: Any, |
| 184 | ) -> AsyncGridOut: |
| 185 | """Get a file from GridFS by ``"filename"`` or metadata fields. |
| 186 | |
| 187 | Returns a version of the file in GridFS whose filename matches |
| 188 | `filename` and whose metadata fields match the supplied keyword |
| 189 | arguments, as an instance of :class:`~gridfs.grid_file.GridOut`. |
| 190 | |
| 191 | Version numbering is a convenience atop the GridFS API provided |
| 192 | by MongoDB. If more than one file matches the query (either by |
| 193 | `filename` alone, by metadata fields, or by a combination of |
| 194 | both), then version ``-1`` will be the most recently uploaded |
| 195 | matching file, ``-2`` the second most recently |
| 196 | uploaded, etc. Version ``0`` will be the first version |
| 197 | uploaded, ``1`` the second version, etc. So if three versions |
| 198 | have been uploaded, then version ``0`` is the same as version |
| 199 | ``-3``, version ``1`` is the same as version ``-2``, and |
| 200 | version ``2`` is the same as version ``-1``. |
| 201 | |
| 202 | Raises :class:`~gridfs.errors.NoFile` if no such version of |
| 203 | that file exists. |
| 204 | |
| 205 | :param filename: ``"filename"`` of the file to get, or `None` |
| 206 | :param version: version of the file to get (defaults |
| 207 | to -1, the most recent version uploaded) |
| 208 | :param session: a |
| 209 | :class:`~pymongo.client_session.AsyncClientSession` |
| 210 | :param kwargs: find files by custom metadata. |
| 211 | |
| 212 | .. versionchanged:: 3.6 |
| 213 | Added ``session`` parameter. |
| 214 | |
| 215 | .. versionchanged:: 3.1 |
| 216 | ``get_version`` no longer ensures indexes. |
| 217 | """ |
| 218 | query = kwargs |
| 219 | if filename is not None: |
| 220 | query["filename"] = filename |
| 221 | |
| 222 | _disallow_transactions(session) |
| 223 | cursor = self._files.find(query, session=session) |
| 224 | if version is None: |
| 225 | version = -1 |
| 226 | if version < 0: |
| 227 | skip = abs(version) - 1 |
| 228 | cursor.limit(-1).skip(skip).sort("uploadDate", DESCENDING) |
| 229 | else: |
| 230 | cursor.limit(-1).skip(version).sort("uploadDate", ASCENDING) |
| 231 | try: |
| 232 | doc = await anext(cursor) |
| 233 | return AsyncGridOut(self._collection, file_document=doc, session=session) |
| 234 | except StopAsyncIteration: |
| 235 | raise NoFile("no version %d for filename %r" % (version, filename)) from None |
no test coverage detected