예제 #1
0
    def _command(self, command, read_preference=None,
        connection=None, callback=None):

        if read_preference is None:
            read_preference = self._read_preference

        client = Client(self, '$cmd')

        client.find_one(command, is_command=True, connection=connection,
            read_preference=read_preference, callback=callback)
예제 #2
0
    def __getattr__(self, name):
        """Get a client collection by name.

        :Parameters:
          - `name`: the name of the collection
        """
        return Client(self, name)
예제 #3
0
    def save(self, safe=True, check_keys=True, callback=None):
        """Save a document

        >>> user = Users()
        >>> user.name = 'should be name'
        >>> user.save()

        :Parameters:
          - `safe` (optional): safe insert operation
          - `check_keys` (optional): check if keys start with '$' or
            contain '.', raising :class:`~pymongo.errors.InvalidName`
            in either case
        - `callback` : method which will be called when save is finished
        """
        pre_save.send(instance=self)

        client = Client(Database(), self.__collection__)
        response, error = yield gen.Task(client.insert,
                                         self.as_dict(),
                                         safe=safe,
                                         check_keys=check_keys)

        self.clean_fields()

        post_save.send(instance=self)

        if callback:
            callback((response, error))
예제 #4
0
    def _command(self,
                 command,
                 read_preference=None,
                 connection=None,
                 callback=None):

        if read_preference is None:
            read_preference = self._read_preference

        client = Client(self, '$cmd')

        client.find_one(command,
                        is_command=True,
                        connection=connection,
                        read_preference=read_preference,
                        callback=callback)
예제 #5
0
파일: manager.py 프로젝트: jibanli/mongotor
    def find_one(self, query, callback):
        client = Client(Database(), self.collection.__collection__)
        result, error = yield gen.Task(client.find_one, query)

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

        callback(instance)
예제 #6
0
파일: manager.py 프로젝트: jibanli/mongotor
    def find(self, query, callback, **kw):
        client = Client(Database(), self.collection.__collection__)
        result, error = yield gen.Task(client.find, query, **kw)

        items = []

        if result:
            for item in result:
                items.append(self.collection.create(item, cleaned=True))

        callback(items)
예제 #7
0
    def remove(self, safe=True, callback=None):
        """Remove a document

        :Parameters:
        - `safe` (optional): safe remove operation
        - `callback` : method which will be called when remove is finished
        """
        pre_remove.send(instance=self)

        client = Client(Database(), self.__collection__)
        response, error = yield gen.Task(client.remove, self._id, safe=safe)

        post_remove.send(instance=self)

        if callback:
            callback((response, error))
예제 #8
0
    def update(self,
               document=None,
               upsert=False,
               safe=True,
               multi=False,
               callback=None,
               force=False):
        """Update a document

        :Parameters:
        - `safe` (optional): safe update operation
        - `callback` : method which will be called when update is finished
        - `force`: if True will overide full document
        """
        if not document and not self.dirty_fields:
            callback(tuple())
            return

        pre_update.send(instance=self)

        if not document:
            if force:
                document = self.as_dict()
            else:
                document = {"$set": self.as_dict(self.dirty_fields)}

        client = Client(Database(), self.__collection__)
        spec = {'_id': self._id}

        response, error = yield gen.Task(client.update,
                                         spec,
                                         document,
                                         upsert=upsert,
                                         safe=safe,
                                         multi=multi)

        self.clean_fields()

        post_update.send(instance=self)

        if callback:
            callback((response, error))
예제 #9
0
 def distinct(self, key, callback, query=None):
     client = Client(Database(), self.collection.__collection__)
     client.find(query).distinct(key, callback=callback)
예제 #10
0
 def count(self, query=None, callback=None):
     client = Client(Database(), self.collection.__collection__)
     client.find(query).count(callback=callback)
예제 #11
0
파일: manager.py 프로젝트: jibanli/mongotor
 def distinct(self, key, callback, query=None):
     client = Client(Database(), self.collection.__collection__)
     client.find(query).distinct(key, callback=callback)
예제 #12
0
파일: manager.py 프로젝트: jibanli/mongotor
 def count(self, query=None, callback=None):
     client = Client(Database(), self.collection.__collection__)
     client.find(query).count(callback=callback)
예제 #13
0
파일: manager.py 프로젝트: jibanli/mongotor
    def truncate(self, callback=None):
        client = Client(Database(), self.collection.__collection__)
        yield gen.Task(client.remove, {})

        if callback:
            callback()