Represents an insert_one operation.
| 83 | |
| 84 | |
| 85 | class InsertOne(Generic[_DocumentType]): |
| 86 | """Represents an insert_one operation.""" |
| 87 | |
| 88 | __slots__ = ( |
| 89 | "_doc", |
| 90 | "_namespace", |
| 91 | ) |
| 92 | |
| 93 | def __init__(self, document: _DocumentType, namespace: Optional[str] = None) -> None: |
| 94 | """Create an InsertOne instance. |
| 95 | |
| 96 | For use with :meth:`~pymongo.asynchronous.collection.AsyncCollection.bulk_write`, :meth:`~pymongo.collection.Collection.bulk_write`, |
| 97 | :meth:`~pymongo.asynchronous.mongo_client.AsyncMongoClient.bulk_write` and :meth:`~pymongo.mongo_client.MongoClient.bulk_write`. |
| 98 | |
| 99 | :param document: The document to insert. If the document is missing an |
| 100 | _id field one will be added. |
| 101 | :param namespace: (optional) The namespace in which to insert a document. |
| 102 | |
| 103 | .. versionchanged:: 4.9 |
| 104 | Added the `namespace` option to support `MongoClient.bulk_write`. |
| 105 | """ |
| 106 | self._doc = document |
| 107 | self._namespace = namespace |
| 108 | |
| 109 | def _add_to_bulk(self, bulkobj: _AgnosticBulk) -> None: |
| 110 | """Add this operation to the _AsyncBulk/_Bulk instance `bulkobj`.""" |
| 111 | bulkobj.add_insert(self._doc) # type: ignore[arg-type] |
| 112 | |
| 113 | def _add_to_client_bulk(self, bulkobj: _AgnosticClientBulk) -> None: |
| 114 | """Add this operation to the _AsyncClientBulk/_ClientBulk instance `bulkobj`.""" |
| 115 | if not self._namespace: |
| 116 | raise InvalidOperation( |
| 117 | "MongoClient.bulk_write requires a namespace to be provided for each write operation" |
| 118 | ) |
| 119 | bulkobj.add_insert( |
| 120 | self._namespace, |
| 121 | self._doc, # type: ignore[arg-type] |
| 122 | ) |
| 123 | |
| 124 | def __repr__(self) -> str: |
| 125 | if self._namespace: |
| 126 | return f"{self.__class__.__name__}({self._doc!r}, {self._namespace!r})" |
| 127 | return f"{self.__class__.__name__}({self._doc!r})" |
| 128 | |
| 129 | def __eq__(self, other: Any) -> bool: |
| 130 | if type(other) == type(self): |
| 131 | return other._doc == self._doc and other._namespace == self._namespace |
| 132 | return NotImplemented |
| 133 | |
| 134 | def __ne__(self, other: Any) -> bool: |
| 135 | return not self == other |
| 136 | |
| 137 | |
| 138 | class _DeleteOp: |
no outgoing calls