コード例 #1
0
ファイル: test_cursor.py プロジェクト: Arion-Dsh/mongotor
    def test_find_returning_fields(self):
        """[CursorTestCase] - Find and return only selectd fields"""

        document1 = {'_id': ObjectId(), 'name': 'should be name 1',
            'comment': [{'author': 'joe'}, {'author': 'ana'}]}
        self._insert_document(document1)

        document2 = {'_id': ObjectId(), 'name': 'should be name 2',
            'comment': [{'author': 'ana'}]}
        self._insert_document(document2)

        document3 = {'_id': ObjectId(), 'name': 'should be name 3',
            'comment': [{'author': 'june'}]}
        self._insert_document(document3)

        cursor = Cursor(Database(), 'cursor_test', {'comment.author': 'joe'},
            ('comment.$.author', ), limit=-1)
        cursor.find(callback=self.stop)

        result, _ = self.wait()

        keys = result.keys()
        keys.sort()

        keys.should.be.equal(['_id', 'comment'])

        str(result['_id']).should.be.equal(str(document1['_id']))
        result['comment'].should.have.length_of(1)
        result['comment'][0]['author'].should.be.equal('joe')
        _.should.be.none
コード例 #2
0
    def command(self,
                command,
                value=1,
                callback=None,
                check=True,
                allowable_errors=[],
                **kwargs):
        """Issue a MongoDB command.

        Send command `command` to the database and return the
        response. If `command` is an instance of :class:`basestring`
        then the command {`command`: `value`} will be sent. Otherwise,
        `command` must be an instance of :class:`dict` and will be
        sent as is.

        Any additional keyword arguments will be added to the final
        command document before it is sent.

        For example, a command like ``{buildinfo: 1}`` can be sent
        using:

        >>> db.command("buildinfo")

        For a command where the value matters, like ``{collstats:
        collection_name}`` we can do:

        >>> db.command("collstats", collection_name)

        For commands that take additional arguments we can use
        kwargs. So ``{filemd5: object_id, root: file_root}`` becomes:

        >>> db.command("filemd5", object_id, root=file_root)

        :Parameters:
          - `command`: document representing the command to be issued,
            or the name of the command (for simple commands only).

            .. note:: the order of keys in the `command` document is
               significant (the "verb" must come first), so commands
               which require multiple keys (e.g. `findandmodify`)
               should use an instance of :class:`~bson.son.SON` or
               a string and kwargs instead of a Python `dict`.

          - `value` (optional): value to use for the command verb when
            `command` is passed as a string
          - `**kwargs` (optional): additional keyword arguments will
            be added to the command document before it is sent

        .. mongodoc:: commands
        """
        if isinstance(command, basestring):
            command = SON([(command, value)])

        command.update(kwargs)

        from mongotor.cursor import Cursor
        cursor = Cursor('$cmd', command, is_command=True)
        cursor.find(limit=-1, callback=callback)
コード例 #3
0
ファイル: client.py プロジェクト: LevonXXL/mongotor
    def find(self, *args, **kwargs):
        """Query the database.

        The `spec` argument is a prototype document that all results
        must match. For example:

        :Parameters:
          - `spec` (optional): a SON object specifying elements which
            must be present for a document to be included in the
            result set
          - `fields` (optional): a list of field names that should be
            returned in the result set ("_id" will always be
            included), or a dict specifying the fields to return
          - `skip` (optional): the number of documents 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, any returned cursor will be
            subject to the normal timeout behavior of the mongod
            process. Otherwise, the returned cursor will never timeout
            at the server. Care should be taken to ensure that cursors
            with timeout turned off are properly closed.
          - `snapshot` (optional): if True, snapshot mode will be used
            for this query. Snapshot mode assures no duplicates are
            returned, or objects missed, which were present at both
            the start and end of the query's execution. For details,
            see the `snapshot documentation
            <http://dochub.mongodb.org/core/snapshot>`_.
          - `tailable` (optional): the result of this find call will
            be a tailable cursor - tailable cursors aren't closed when
            the last data is retrieved but are kept open and the
            cursors location marks the final document's position. if
            more data is received iteration of the cursor will
            continue from the last document received. For details, see
            the `tailable cursor documentation
            <http://www.mongodb.org/display/DOCS/Tailable+Cursors>`_.
          - `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 documents
            examined when performing the query
          - `read_preferences` (optional): The read preference for
            this query.
        """

        log.debug("mongo: db.{0}.find({spec}).limit({limit}).sort({sort})".format(
            self._collection_name,
            spec=args[0] if args else {},
            sort=kwargs.get('sort', {}),
            limit=kwargs.get('limit', '')
        ))
        cursor = Cursor(self._database, self._collection,
            *args, **kwargs)

        if 'callback' in kwargs:
            cursor.find(callback=kwargs['callback'])
        else:
            return cursor
コード例 #4
0
ファイル: client.py プロジェクト: jibanli/mongotor
    def find(self, *args, **kwargs):
        """Query the database.

        The `spec` argument is a prototype document that all results
        must match. For example:

        :Parameters:
          - `spec` (optional): a SON object specifying elements which
            must be present for a document to be included in the
            result set
          - `fields` (optional): a list of field names that should be
            returned in the result set ("_id" will always be
            included), or a dict specifying the fields to return
          - `skip` (optional): the number of documents 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, any returned cursor will be
            subject to the normal timeout behavior of the mongod
            process. Otherwise, the returned cursor will never timeout
            at the server. Care should be taken to ensure that cursors
            with timeout turned off are properly closed.
          - `snapshot` (optional): if True, snapshot mode will be used
            for this query. Snapshot mode assures no duplicates are
            returned, or objects missed, which were present at both
            the start and end of the query's execution. For details,
            see the `snapshot documentation
            <http://dochub.mongodb.org/core/snapshot>`_.
          - `tailable` (optional): the result of this find call will
            be a tailable cursor - tailable cursors aren't closed when
            the last data is retrieved but are kept open and the
            cursors location marks the final document's position. if
            more data is received iteration of the cursor will
            continue from the last document received. For details, see
            the `tailable cursor documentation
            <http://www.mongodb.org/display/DOCS/Tailable+Cursors>`_.
          - `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 documents
            examined when performing the query
          - `read_preferences` (optional): The read preference for
            this query.
        """

        log.debug(
            "mongo: db.{0}.find({spec}).limit({limit}).sort({sort})".format(
                self._collection_name,
                spec=args[0] if args else {},
                sort=kwargs.get('sort', {}),
                limit=kwargs.get('limit', '')))
        cursor = Cursor(self._database, self._collection, *args, **kwargs)

        if 'callback' in kwargs:
            cursor.find(callback=kwargs['callback'])
        else:
            return cursor
コード例 #5
0
ファイル: database.py プロジェクト: pauloalem/mongotor
    def command(self, command, value=1, callback=None, check=True,
        allowable_errors=[], **kwargs):
        """Issue a MongoDB command.

        Send command `command` to the database and return the
        response. If `command` is an instance of :class:`basestring`
        then the command {`command`: `value`} will be sent. Otherwise,
        `command` must be an instance of :class:`dict` and will be
        sent as is.

        Any additional keyword arguments will be added to the final
        command document before it is sent.

        For example, a command like ``{buildinfo: 1}`` can be sent
        using:

        >>> db.command("buildinfo")

        For a command where the value matters, like ``{collstats:
        collection_name}`` we can do:

        >>> db.command("collstats", collection_name)

        For commands that take additional arguments we can use
        kwargs. So ``{filemd5: object_id, root: file_root}`` becomes:

        >>> db.command("filemd5", object_id, root=file_root)

        :Parameters:
          - `command`: document representing the command to be issued,
            or the name of the command (for simple commands only).

            .. note:: the order of keys in the `command` document is
               significant (the "verb" must come first), so commands
               which require multiple keys (e.g. `findandmodify`)
               should use an instance of :class:`~bson.son.SON` or
               a string and kwargs instead of a Python `dict`.

          - `value` (optional): value to use for the command verb when
            `command` is passed as a string
          - `**kwargs` (optional): additional keyword arguments will
            be added to the command document before it is sent

        .. mongodoc:: commands
        """
        if isinstance(command, basestring):
            command = SON([(command, value)])

        command.update(kwargs)

        from mongotor.cursor import Cursor
        cursor = Cursor('$cmd', command, is_command=True)
        cursor.find(limit=-1, callback=callback)
コード例 #6
0
ファイル: test_cursor.py プロジェクト: sansa1839/mongotor
    def test_find_document_whitout_spec(self):
        """[CursorTestCase] - Find one document without spec"""

        document = {'_id': ObjectId(), 'name': 'should be name'}
        self._insert_document(document)

        cursor = Cursor(database=Database(), collection='cursor_test', limit=-1)
        cursor.find(callback=self.stop)

        result, error = self.wait()

        result['_id'].should.be.equal(document['_id'])
        result['name'].should.be.equal(document['name'])
        error.should.be.none
コード例 #7
0
    def test_find_document_whitout_spect(self):
        """[CursorTestCase] - Find one document without spec"""

        document = {'_id': ObjectId(), 'name': 'should be name'}
        self._insert_document(document)

        cursor = Cursor('cursor_test')
        cursor.find(limit=-1, callback=self.stop)

        result, error = self.wait()

        result['_id'].should.be.equal(document['_id'])
        result['name'].should.be.equal(document['name'])
        error.should.be.none
コード例 #8
0
    def test_find_returning_fields(self):
        """[CursorTestCase] - Find and return only selectd fields"""

        document1 = {
            '_id': ObjectId(),
            'name': 'should be name 1',
            'comment': [{
                'author': 'joe'
            }, {
                'author': 'ana'
            }]
        }
        self._insert_document(document1)

        document2 = {
            '_id': ObjectId(),
            'name': 'should be name 2',
            'comment': [{
                'author': 'ana'
            }]
        }
        self._insert_document(document2)

        document3 = {
            '_id': ObjectId(),
            'name': 'should be name 3',
            'comment': [{
                'author': 'june'
            }]
        }
        self._insert_document(document3)

        cursor = Cursor(Database(),
                        'cursor_test', {'comment.author': 'joe'},
                        ('comment.$.author', ),
                        limit=-1)
        cursor.find(callback=self.stop)

        result, _ = self.wait()

        keys = result.keys()
        keys.sort()

        keys.should.be.equal(['_id', 'comment'])

        str(result['_id']).should.be.equal(str(document1['_id']))
        result['comment'].should.have.length_of(1)
        result['comment'][0]['author'].should.be.equal('joe')
        _.should.be.none
コード例 #9
0
    def find_one(self, query, callback):

        cursor = Cursor(self.collection.__collection__, query)
        result, error = yield gen.Task(cursor.find, limit=-1)

        instance = None
        if result:
            instance = self.collection.create(result)

        callback(instance)
コード例 #10
0
    def test_find_document_by_id(self):
        """[CursorTestCase] - Find document by id"""

        document1 = {'_id': ObjectId(), 'name': 'should be name 1', 'size': 1}
        self._insert_document(document1)

        document2 = {'_id': ObjectId(), 'name': 'should be name 2', 'size': 2}
        self._insert_document(document2)

        document3 = {'_id': ObjectId(), 'name': 'should be name 3', 'size': 3}
        self._insert_document(document3)

        cursor = Cursor(Database(), 'cursor_test', document2['_id'], limit=-1)
        cursor.find(callback=self.stop)

        result, error = self.wait()

        str(result['_id']).should.be.equal(str(document2['_id']))
        error.should.be.none
コード例 #11
0
ファイル: test_cursor.py プロジェクト: pauloalem/mongotor
    def test_find_document_by_id(self):
        """[CursorTestCase] - Find document by id"""

        document1 = {'_id': ObjectId(), 'name': 'should be name 1', 'size': 1}
        self._insert_document(document1)

        document2 = {'_id': ObjectId(), 'name': 'should be name 2', 'size': 2}
        self._insert_document(document2)

        document3 = {'_id': ObjectId(), 'name': 'should be name 3', 'size': 3}
        self._insert_document(document3)

        cursor = Cursor('cursor_test', document2['_id'])
        cursor.find(limit=-1, callback=self.stop)

        result, error = self.wait()

        str(result['_id']).should.be.equal(str(document2['_id']))
        error.should.be.none
コード例 #12
0
    def find(self, query, callback, **kw):
        cursor = Cursor(self.collection.__collection__, query)
        result, error = yield gen.Task(cursor.find, **kw)

        items = []

        if result:
            for item in result:
                items.append(self.collection.create(item))

        callback(items)
コード例 #13
0
ファイル: test_cursor.py プロジェクト: sansa1839/mongotor
    def test_find_documents_with_spec(self):
        """[CursorTestCase] - Find documents with spec"""

        document1 = {'_id': ObjectId(), 'name': 'should be name 1', 'flag': 1}
        self._insert_document(document1)

        document2 = {'_id': ObjectId(), 'name': 'should be name 2', 'flag': 2}
        self._insert_document(document2)

        document3 = {'_id': ObjectId(), 'name': 'should be name 3', 'flag': 1}
        self._insert_document(document3)

        cursor = Cursor({'flag': 1}, database=Database(), collection='cursor_test', limit=2)
        cursor.find(callback=self.stop)

        result, error = self.wait()

        result.should.have.length_of(2)
        str(result[0]['_id']).should.be.equal(str(document1['_id']))
        str(result[1]['_id']).should.be.equal(str(document3['_id']))
        error.should.be.none
コード例 #14
0
    def test_find_documents_with_spec(self):
        """[CursorTestCase] - Find documents with spec"""

        document1 = {'_id': ObjectId(), 'name': 'should be name 1', 'flag': 1}
        self._insert_document(document1)

        document2 = {'_id': ObjectId(), 'name': 'should be name 2', 'flag': 2}
        self._insert_document(document2)

        document3 = {'_id': ObjectId(), 'name': 'should be name 3', 'flag': 1}
        self._insert_document(document3)

        cursor = Cursor(Database(), 'cursor_test', {'flag': 1}, limit=2)
        cursor.find(callback=self.stop)

        result, error = self.wait()

        result.should.have.length_of(2)
        str(result[0]['_id']).should.be.equal(str(document1['_id']))
        str(result[1]['_id']).should.be.equal(str(document3['_id']))
        error.should.be.none
コード例 #15
0
    def test_find_documents_with_limit(self):
        """[CursorTestCase] - Find documents with limit"""

        document1 = {'_id': ObjectId(), 'name': 'should be name 1'}
        self._insert_document(document1)

        document2 = {'_id': ObjectId(), 'name': 'should be name 2'}
        self._insert_document(document2)

        document3 = {'_id': ObjectId(), 'name': 'should be name 3'}
        self._insert_document(document3)

        cursor = Cursor(database=Database(), collection='cursor_test', limit=2)
        cursor.find(callback=self.stop)

        result, error = self.wait()

        result.should.have.length_of(2)
        str(result[0]['_id']).should.be.equal(str(document1['_id']))
        str(result[1]['_id']).should.be.equal(str(document2['_id']))
        error.should.be.none
コード例 #16
0
ファイル: test_cursor.py プロジェクト: pauloalem/mongotor
    def test_find_documents_ordering_descending_by_field(self):
        """[CursorTestCase] - Find documents order descending by field"""

        document1 = {'_id': ObjectId(), 'name': 'should be name 1', 'size': 1}
        self._insert_document(document1)

        document2 = {'_id': ObjectId(), 'name': 'should be name 2', 'size': 2}
        self._insert_document(document2)

        document3 = {'_id': ObjectId(), 'name': 'should be name 3', 'size': 3}
        self._insert_document(document3)

        cursor = Cursor('cursor_test')
        cursor.find(limit=2, sort={'size': DESCENDING}, callback=self.stop)

        result, error = self.wait()

        result.should.have.length_of(2)
        str(result[0]['_id']).should.be.equal(str(document3['_id']))
        str(result[1]['_id']).should.be.equal(str(document2['_id']))
        error.should.be.none
コード例 #17
0
ファイル: test_cursor.py プロジェクト: pauloalem/mongotor
    def test_find_documents_with_limit(self):
        """[CursorTestCase] - Find documents with limit"""

        document1 = {'_id': ObjectId(), 'name': 'should be name 1'}
        self._insert_document(document1)

        document2 = {'_id': ObjectId(), 'name': 'should be name 2'}
        self._insert_document(document2)

        document3 = {'_id': ObjectId(), 'name': 'should be name 3'}
        self._insert_document(document3)

        cursor = Cursor('cursor_test')
        cursor.find(limit=2, callback=self.stop)

        result, error = self.wait()

        result.should.have.length_of(2)
        str(result[0]['_id']).should.be.equal(str(document1['_id']))
        str(result[1]['_id']).should.be.equal(str(document2['_id']))
        error.should.be.none
コード例 #18
0
    def test_find_documents_ordering_descending_by_field(self):
        """[CursorTestCase] - Find documents order descending by field"""

        document1 = {'_id': ObjectId(), 'name': 'should be name 1', 'size': 1}
        self._insert_document(document1)

        document2 = {'_id': ObjectId(), 'name': 'should be name 2', 'size': 2}
        self._insert_document(document2)

        document3 = {'_id': ObjectId(), 'name': 'should be name 3', 'size': 3}
        self._insert_document(document3)

        cursor = Cursor('cursor_test')
        cursor.find(limit=2, sort={'size': DESCENDING}, callback=self.stop)

        result, error = self.wait()

        result.should.have.length_of(2)
        str(result[0]['_id']).should.be.equal(str(document3['_id']))
        str(result[1]['_id']).should.be.equal(str(document2['_id']))
        error.should.be.none