def __getattr__(self, name): """Get a client collection by name. :Parameters: - `name`: the name of the collection """ return Client(self, name)
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))
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)
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)
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)
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))
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))
def distinct(self, key, callback, query=None): client = Client(Database(), self.collection.__collection__) client.find(query).distinct(key, callback=callback)
def count(self, query=None, callback=None): client = Client(Database(), self.collection.__collection__) client.find(query).count(callback=callback)
def truncate(self, callback=None): client = Client(Database(), self.collection.__collection__) yield gen.Task(client.remove, {}) if callback: callback()