A cursor / iterator for returning GridOut objects as the result of an arbitrary query against the GridFS files collection.
| 1922 | |
| 1923 | |
| 1924 | class GridOutCursor(Cursor): # type: ignore[type-arg] |
| 1925 | """A cursor / iterator for returning GridOut objects as the result |
| 1926 | of an arbitrary query against the GridFS files collection. |
| 1927 | """ |
| 1928 | |
| 1929 | def __init__( |
| 1930 | self, |
| 1931 | collection: Collection[Any], |
| 1932 | filter: Optional[Mapping[str, Any]] = None, |
| 1933 | skip: int = 0, |
| 1934 | limit: int = 0, |
| 1935 | no_cursor_timeout: bool = False, |
| 1936 | sort: Optional[Any] = None, |
| 1937 | batch_size: int = 0, |
| 1938 | session: Optional[ClientSession] = None, |
| 1939 | ) -> None: |
| 1940 | """Create a new cursor, similar to the normal |
| 1941 | :class:`~pymongo.cursor.Cursor`. |
| 1942 | |
| 1943 | Should not be called directly by application developers - see |
| 1944 | the :class:`~gridfs.GridFS` method :meth:`~gridfs.GridFS.find` instead. |
| 1945 | |
| 1946 | .. versionadded 2.7 |
| 1947 | |
| 1948 | .. seealso:: The MongoDB documentation on `cursors <https://dochub.mongodb.org/core/cursors>`_. |
| 1949 | """ |
| 1950 | _disallow_transactions(session) |
| 1951 | collection = _clear_entity_type_registry(collection) |
| 1952 | |
| 1953 | # Hold on to the base "fs" collection to create GridOut objects later. |
| 1954 | self._root_collection = collection |
| 1955 | |
| 1956 | super().__init__( |
| 1957 | collection.files, |
| 1958 | filter, |
| 1959 | skip=skip, |
| 1960 | limit=limit, |
| 1961 | no_cursor_timeout=no_cursor_timeout, |
| 1962 | sort=sort, |
| 1963 | batch_size=batch_size, |
| 1964 | session=session, |
| 1965 | ) |
| 1966 | |
| 1967 | def next(self) -> GridOut: |
| 1968 | """Get next GridOut object from cursor.""" |
| 1969 | _disallow_transactions(self.session) |
| 1970 | next_file = super().next() |
| 1971 | return GridOut(self._root_collection, file_document=next_file, session=self.session) |
| 1972 | |
| 1973 | def to_list(self, length: Optional[int] = None) -> list[GridOut]: |
| 1974 | """Convert the cursor to a list.""" |
| 1975 | if length is None: |
| 1976 | return [x for x in self] # noqa: C416,RUF100 |
| 1977 | if length < 1: |
| 1978 | raise ValueError("to_list() length must be greater than 0") |
| 1979 | ret = [] |
| 1980 | for _ in range(length): |
| 1981 | ret.append(self.next()) |
no outgoing calls