def insert(self, doc_or_docs, safe=True, check_keys=True, callback=None): """Insert a document :Parameters: - `doc_or_docs`: a document or list of documents to be inserted - `safe` (optional): check that the insert succeeded? - `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 """ if isinstance(doc_or_docs, dict): doc_or_docs = [doc_or_docs] assert isinstance(doc_or_docs, list) message_insert = message.insert(self._collection_name, doc_or_docs, check_keys, safe, {}) log.debug("mongo: db.{0}.insert({1})".format(self._collection_name, doc_or_docs)) node = yield gen.Task(self._database.get_node, ReadPreference.PRIMARY) connection = yield gen.Task(node.connection) response, error = yield gen.Task(connection.send_message, message_insert, safe) if callback: callback((response, error))
def _insert_document(self, document): message_insert = message.insert('mongotor_test.cursor_test', [document], True, True, {}) node = Database().get_node(ReadPreference.PRIMARY) node.connection(self.stop) connection = self.wait() connection.send_message(message_insert, with_last_error=True, callback=self.stop) self.wait()
def test_return_integrity_error_when_mongo_return_err(self): """[ConnectionTestCase] - Returns IntegrityError when mongo return a message with err""" object_id = ObjectId() message_insert = message.insert('mongotor_test.articles', [{'_id': object_id}], False, True, {}) self.conn.send_message(message_insert, True, callback=self.stop) self.wait() self.conn.send_message(message_insert, True, callback=self.stop) self.wait.when.called_with().throw(IntegrityError)
def _insert_document(self, document): message_insert = message.insert('mongotor_test.cursor_test', [document], True, True, {}) Database().get_node(ReadPreference.PRIMARY, callback=self.stop) node = self.wait() node.connection(self.stop) connection = self.wait() connection.send_message(message_insert, with_last_error=True, callback=self.stop) self.wait()
def test_return_integrity_error_when_mongo_return_err(self): """[ConnectionTestCase] - Returns IntegrityError when mongo return a message with err""" object_id = ObjectId() message_insert = message.insert('mongotor_test.articles', [{ '_id': object_id }], False, True, {}) self.conn.send_message(message_insert, True, callback=self.stop) self.wait() self.conn.send_message(message_insert, True, callback=self.stop) self.wait.when.called_with().throw(IntegrityError)
def save(self, safe=True, callback=None): """Save a document :Parameters: - `safe` (optional): safe insert operation - `callback` : method which will be called when save is finished """ pre_save.send(instance=self) database = Database() collection_name = database.get_collection_name(self.__collection__) message_insert = message.insert(collection_name, [self.as_dict()], True, safe, {}) response, error = yield gen.Task(database.send_message, message_insert) post_save.send(instance=self) if callback: callback((response, error))
def _insert_document(self, document): message_insert = message.insert('mongotor_test.cursor_test', [document], True, True, {}) Database().send_message(message_insert, callback=self.stop) self.wait()