def insert(self, doc_or_docs, manipulate=True, safe=False, check_keys=True): """Insert a document(s) into this collection. If manipulate is set the document(s) are manipulated using any SONManipulators that have been added to this database. Returns the _id of the inserted document or a list of _ids of the inserted documents. If the document(s) does not already contain an '_id' one will be added. If `safe` is True then the insert will be checked for errors, raising OperationFailure if one occurred. Safe inserts wait for a response from the database, while normal inserts do not. :Parameters: - `doc_or_docs`: a SON object or list of SON objects to be inserted - `manipulate` (optional): manipulate the documents before inserting? - `safe` (optional): check that the insert succeeded? - `check_keys` (optional): check if keys start with '$' or contain '.', raising `pymongo.errors.InvalidName` in either case .. versionchanged:: 1.1 Bulk insert works with any iterable """ docs = doc_or_docs if isinstance(docs, types.DictType): docs = [docs] if manipulate: docs = [self.__database._fix_incoming(doc, self) for doc in docs] self.__database.connection._send_message( message.insert(self.__full_name, docs, check_keys, safe), safe) ids = [doc.get("_id", None) for doc in docs] return len(ids) == 1 and ids[0] or ids
def insert(self, doc_or_docs, manipulate=True, safe=False, check_keys=True): """Insert a document(s) into this collection. If manipulate is set the document(s) are manipulated using any SONManipulators that have been added to this database. Returns the _id of the inserted document or a list of _ids of the inserted documents. If the document(s) does not already contain an '_id' one will be added. If `safe` is True then the insert will be checked for errors, raising OperationFailure if one occurred. Safe inserts wait for a response from the database, while normal inserts do not. :Parameters: - `doc_or_docs`: a SON object or list of SON objects to be inserted - `manipulate` (optional): manipulate the documents before inserting? - `safe` (optional): check that the insert succeeded? - `check_keys` (optional): check if keys start with '$' or contain '.', raising `pymongo.errors.InvalidName` in either case """ docs = doc_or_docs if isinstance(docs, types.DictType): docs = [docs] if manipulate: docs = [self.__database._fix_incoming(doc, self) for doc in docs] self.__database.connection()._send_message( message.insert(self.full_name(), docs, check_keys, safe), safe) ids = [doc.get("_id", None) for doc in docs] return len(ids) == 1 and ids[0] or ids
def insert(self, doc_or_docs, manipulate=True, safe=False, check_keys=True, callback=None, **kwargs): """Insert a document(s) into this collection. If `manipulate` is set, the document(s) are manipulated using any :class:`~pymongo.son_manipulator.SONManipulator` instances that have been added to this :class:`~pymongo.database.Database`. Returns the ``"_id"`` of the inserted document or a list of ``"_id"`` values of the inserted documents. If the document(s) does not already contain an ``"_id"`` one will be added. If `safe` is ``True`` then the insert will be checked for errors, raising :class:`~pymongo.errors.OperationFailure` if one occurred. Safe inserts wait for a response from the database, while normal inserts do not. Any additional keyword arguments imply ``safe=True``, and will be used as options for the resultant `getLastError` command. For example, to wait for replication to 3 nodes, pass ``w=3``. :Parameters: - `doc_or_docs`: a document or list of documents to be inserted - `manipulate` (optional): manipulate the documents before inserting? - `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 - `**kwargs` (optional): any additional arguments imply ``safe=True``, and will be used as options for the `getLastError` command .. mongodoc:: insert """ if not isinstance(safe, bool): raise TypeError("safe must be an instance of bool") if not callable(callback): raise TypeError("callback must be callable") docs = doc_or_docs # return_one = False if isinstance(docs, dict): # return_one = True docs = [docs] # if manipulate: # docs = [self.__database._fix_incoming(doc, self) for doc in docs] self.__limit = None if kwargs: safe = True connection = self.__pool.connection() connection.send_message( message.insert(self.full_collection_name, docs, check_keys, safe, kwargs), callback=self.async_callback(self._handle_response, orig_callback=callback))
def _handle_connection(connection): try: connection.send_message( message.insert(self.full_collection_name, docs, check_keys, safe, kwargs), callback=callback) except: connection.close() raise
def insert(self, doc_or_docs, manipulate=True, safe=True, check_keys=True, callback=None, **kwargs): """Insert a document(s) into this collection. If `manipulate` is set, the document(s) are manipulated using any :class:`~pymongo.son_manipulator.SONManipulator` instances that have been added to this :class:`~pymongo.database.Database`. Returns the ``"_id"`` of the inserted document or a list of ``"_id"`` values of the inserted documents. If the document(s) does not already contain an ``"_id"`` one will be added. If `safe` is ``True`` then the insert will be checked for errors, raising :class:`~pymongo.errors.OperationFailure` if one occurred. Safe inserts wait for a response from the database, while normal inserts do not. Any additional keyword arguments imply ``safe=True``, and will be used as options for the resultant `getLastError` command. For example, to wait for replication to 3 nodes, pass ``w=3``. :Parameters: - `doc_or_docs`: a document or list of documents to be inserted - `manipulate` (optional): manipulate the documents before inserting? - `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 - `**kwargs` (optional): any additional arguments imply ``safe=True``, and will be used as options for the `getLastError` command .. mongodoc:: insert """ if not isinstance(safe, bool): raise TypeError("safe must be an instance of bool") docs = doc_or_docs # return_one = False if isinstance(docs, dict): # return_one = True docs = [docs] # if manipulate: # docs = [self.__database._fix_incoming(doc, self) for doc in docs] self.__limit = None if kwargs: safe = True if safe and not callable(callback): raise TypeError("callback must be callable") if not safe and callback is not None: raise TypeError("callback can not be used with safe=False") if callback: callback = functools.partial(self._handle_response, orig_callback=callback) connection = self.__pool.connection() try: connection.send_message( message.insert(self.full_collection_name, docs, check_keys, safe, kwargs), callback=callback) except: connection.close() raise