def test_grid_out_cursor_options(self):
        self.assertRaises(TypeError, GridOutCursor.__init__, self.db.fs, {},
                          projection={"filename": 1})

        cursor = GridOutCursor(self.db.fs, {})
        cursor_clone = cursor.clone()
        self.assertEqual(cursor_clone.__dict__, cursor.__dict__)

        self.assertRaises(NotImplementedError, cursor.add_option, 0)
        self.assertRaises(NotImplementedError, cursor.remove_option, 0)
Пример #2
0
    def find(self, *args: Any, **kwargs: Any) -> GridOutCursor:
        """Query GridFS for files.

        Returns a cursor that iterates across files matching
        arbitrary queries on the files collection. Can be combined
        with other modifiers for additional control. For example::

          for grid_out in fs.find({"filename": "lisa.txt"},
                                  no_cursor_timeout=True):
              data = grid_out.read()

        would iterate through all versions of "lisa.txt" stored in GridFS.
        Note that setting no_cursor_timeout to True may be important to
        prevent the cursor from timing out during long multi-file processing
        work.

        As another example, the call::

          most_recent_three = fs.find().sort("uploadDate", -1).limit(3)

        would return a cursor to the three most recently uploaded files
        in GridFS.

        Follows a similar interface to
        :meth:`~pymongo.collection.Collection.find`
        in :class:`~pymongo.collection.Collection`.

        If a :class:`~pymongo.client_session.ClientSession` is passed to
        :meth:`find`, all returned :class:`~gridfs.grid_file.GridOut` instances
        are associated with that session.

        :Parameters:
          - `filter` (optional): A query document that selects which files
            to include in the result set. Can be an empty document to include
            all files.
          - `skip` (optional): the number of files to omit (from
            the start of the result set) when returning the results
          - `limit` (optional): the maximum number of results to
            return
          - `no_cursor_timeout` (optional): if False (the default), any
            returned cursor is closed by the server after 10 minutes of
            inactivity. If set to True, the returned cursor will never
            time out on the server. Care should be taken to ensure that
            cursors with no_cursor_timeout turned on are properly closed.
          - `sort` (optional): a list of (key, direction) pairs
            specifying the sort order for this query. See
            :meth:`~pymongo.cursor.Cursor.sort` for details.

        Raises :class:`TypeError` if any of the arguments are of
        improper type. Returns an instance of
        :class:`~gridfs.grid_file.GridOutCursor`
        corresponding to this query.

        .. versionchanged:: 3.0
           Removed the read_preference, tag_sets, and
           secondary_acceptable_latency_ms options.
        .. versionadded:: 2.7
        .. seealso:: The MongoDB documentation on `find <https://dochub.mongodb.org/core/find>`_.
        """
        return GridOutCursor(self.__collection, *args, **kwargs)
 def test_delete_not_initialized(self):
     # Creating a cursor with invalid arguments will not run __init__
     # but will still call __del__.
     cursor = GridOutCursor.__new__(GridOutCursor)  # Skip calling __init__
     with self.assertRaises(TypeError):
         cursor.__init__(self.db.fs.files, {}, {"_id": True})
     cursor.__del__()  # no error
    def test_grid_out_cursor_options(self):
        self.assertRaises(TypeError, GridOutCursor.__init__, self.db.fs, {}, tailable=True)
        self.assertRaises(TypeError, GridOutCursor.__init__, self.db.fs, {}, fields={"filename": 1})

        cursor = GridOutCursor(self.db.fs, {})
        min_ms = self.db.fs.files.secondary_acceptable_latency_ms
        new_ms = cursor._Cursor__secondary_acceptable_latency_ms
        self.assertEqual(min_ms, new_ms)
        cursor = GridOutCursor(self.db.fs, {}, secondary_acceptable_latency_ms=100)
        min_ms = self.db.fs.files.secondary_acceptable_latency_ms
        new_ms = cursor._Cursor__secondary_acceptable_latency_ms
        self.assertNotEqual(min_ms, new_ms)
        cursor_clone = cursor.clone()
        self.assertEqual(cursor_clone.__dict__, cursor.__dict__)

        self.assertRaises(NotImplementedError, cursor.add_option, 0)
        self.assertRaises(NotImplementedError, cursor.remove_option, 0)
Пример #5
0
    def test_grid_out_cursor_options(self):
        self.assertRaises(TypeError, GridOutCursor.__init__, self.db.fs, {},
                                                    tailable=True)
        self.assertRaises(TypeError, GridOutCursor.__init__, self.db.fs, {},
                                                    fields={"filename":1})

        cursor = GridOutCursor(self.db.fs, {})
        min_ms = self.db.fs.files.secondary_acceptable_latency_ms
        new_ms = cursor._Cursor__secondary_acceptable_latency_ms
        self.assertEqual(min_ms, new_ms)
        cursor = GridOutCursor(self.db.fs, {},
                               secondary_acceptable_latency_ms=100)
        min_ms = self.db.fs.files.secondary_acceptable_latency_ms
        new_ms = cursor._Cursor__secondary_acceptable_latency_ms
        self.assertNotEqual(min_ms, new_ms)
        cursor_clone = cursor.clone()
        self.assertEqual(cursor_clone.__dict__, cursor.__dict__)

        self.assertRaises(NotImplementedError, cursor.add_option, 0)
        self.assertRaises(NotImplementedError, cursor.remove_option, 0)
Пример #6
0
    def find(self, *args: Any, **kwargs: Any) -> GridOutCursor:
        """Find and return the files collection documents that match ``filter``

        Returns a cursor that iterates across files matching
        arbitrary queries on the files collection. Can be combined
        with other modifiers for additional control.

        For example::

          for grid_data in fs.find({"filename": "lisa.txt"},
                                  no_cursor_timeout=True):
              data = grid_data.read()

        would iterate through all versions of "lisa.txt" stored in GridFS.
        Note that setting no_cursor_timeout to True may be important to
        prevent the cursor from timing out during long multi-file processing
        work.

        As another example, the call::

          most_recent_three = fs.find().sort("uploadDate", -1).limit(3)

        would return a cursor to the three most recently uploaded files
        in GridFS.

        Follows a similar interface to
        :meth:`~pymongo.collection.Collection.find`
        in :class:`~pymongo.collection.Collection`.

        If a :class:`~pymongo.client_session.ClientSession` is passed to
        :meth:`find`, all returned :class:`~gridfs.grid_file.GridOut` instances
        are associated with that session.

        :Parameters:
          - `filter`: Search query.
          - `batch_size` (optional): The number of documents to return per
            batch.
          - `limit` (optional): The maximum number of documents to return.
          - `no_cursor_timeout` (optional): The server normally times out idle
            cursors after an inactivity period (10 minutes) to prevent excess
            memory use. Set this option to True prevent that.
          - `skip` (optional): The number of documents to skip before
            returning.
          - `sort` (optional): The order by which to sort results. Defaults to
            None.
        """
        return GridOutCursor(self._collection, *args, **kwargs)
Пример #7
0
    def find(self, *args, **kwargs):
        """Query GridFS for files.

        Returns a cursor that iterates across files matching
        arbitrary queries on the files collection. Can be combined
        with other modifiers for additional control. For example::

          for grid_out in fs.find({"filename": "lisa.txt"}, timeout=False):
              data = grid_out.read()

        would iterate through all versions of "lisa.txt" stored in GridFS.
        Note that setting timeout to False may be important to prevent the
        cursor from timing out during long multi-file processing work.

        As another example, the call::

          most_recent_three = fs.find().sort("uploadDate", -1).limit(3)

        would return a cursor to the three most recently uploaded files
        in GridFS.

        Follows a similar interface to
        :meth:`~pymongo.collection.Collection.find`
        in :class:`~pymongo.collection.Collection`.

        :Parameters:
          - `spec` (optional): a SON object specifying elements which
            must be present for a document to be included in the
            result set
          - `skip` (optional): the number of files to omit (from
            the start of the result set) when returning the results
          - `limit` (optional): the maximum number of results to
            return
          - `timeout` (optional): if True (the default), any returned
            cursor is closed by the server after 10 minutes of
            inactivity. If set to False, the returned cursor will never
            time out on the server. Care should be taken to ensure that
            cursors with timeout turned off are properly closed.
          - `sort` (optional): a list of (key, direction) pairs
            specifying the sort order for this query. See
            :meth:`~pymongo.cursor.Cursor.sort` for details.
          - `max_scan` (optional): limit the number of file documents
            examined when performing the query
          - `read_preference` (optional): The read preference for
            this query.
          - `tag_sets` (optional): The tag sets for this query.
          - `secondary_acceptable_latency_ms` (optional): Any replica-set
            member whose ping time is within secondary_acceptable_latency_ms of
            the nearest member may accept reads. Default 15 milliseconds.
            **Ignored by mongos** and must be configured on the command line.
            See the localThreshold_ option for more information.
          - `compile_re` (optional): if ``False``, don't attempt to compile
            BSON regex objects into Python regexes. Return instances of
            :class:`~bson.regex.Regex` instead.
          - `filter` (optional): a SON object specifying elements which
            must be present for a document to be included in the
            result set. Takes precedence over `spec`.
          - `no_cursor_timeout` (optional): if False (the default), any
            returned cursor is closed by the server after 10 minutes of
            inactivity. If set to True, the returned cursor will never
            time out on the server. Care should be taken to ensure that
            cursors with no_cursor_timeout turned on are properly closed.
            Takes precedence over `timeout`.

        Raises :class:`TypeError` if any of the arguments are of
        improper type. Returns an instance of
        :class:`~gridfs.grid_file.GridOutCursor`
        corresponding to this query.

        .. versionadded:: 2.7
        .. mongodoc:: find
        .. _localThreshold: http://docs.mongodb.org/manual/reference/mongos/#cmdoption-mongos--localThreshold
        """
        return GridOutCursor(self.__collection, *args, **kwargs)