MCPcopy Index your code
hub / github.com/mongodb/mongo-python-driver / ReplaceOne

Class ReplaceOne

pymongo/operations.py:318–458  ·  view source on GitHub ↗

Represents a replace_one operation.

Source from the content-addressed store, hash-verified

316
317
318class ReplaceOne(Generic[_DocumentType]):
319 """Represents a replace_one operation."""
320
321 __slots__ = (
322 "_filter",
323 "_doc",
324 "_upsert",
325 "_collation",
326 "_hint",
327 "_namespace",
328 "_sort",
329 )
330
331 def __init__(
332 self,
333 filter: Mapping[str, Any],
334 replacement: Union[_DocumentType, RawBSONDocument],
335 upsert: Optional[bool] = None,
336 collation: Optional[_CollationIn] = None,
337 hint: Optional[_IndexKeyHint] = None,
338 namespace: Optional[str] = None,
339 sort: Optional[Mapping[str, Any]] = None,
340 ) -> None:
341 """Create a ReplaceOne instance.
342
343 For use with :meth:`~pymongo.asynchronous.collection.AsyncCollection.bulk_write`, :meth:`~pymongo.collection.Collection.bulk_write`,
344 :meth:`~pymongo.asynchronous.mongo_client.AsyncMongoClient.bulk_write` and :meth:`~pymongo.mongo_client.MongoClient.bulk_write`.
345
346 :param filter: A query that matches the document to replace.
347 :param replacement: The new document.
348 :param upsert: If ``True``, perform an insert if no documents
349 match the filter.
350 :param collation: An instance of
351 :class:`~pymongo.collation.Collation`.
352 :param hint: An index to use to support the query
353 predicate specified either by its string name, or in the same
354 format as passed to
355 :meth:`~pymongo.asynchronous.collection.AsyncCollection.create_index` or :meth:`~pymongo.collection.Collection.create_index` (e.g.
356 ``[('field', ASCENDING)]``). This option is only supported on
357 MongoDB 4.2 and above.
358 :param sort: Specify which document the operation updates if the query matches
359 multiple documents. The first document matched by the sort order will be updated.
360 :param namespace: (optional) The namespace in which to replace a document.
361
362 .. versionchanged:: 4.10
363 Added ``sort`` option.
364 .. versionchanged:: 4.9
365 Added the `namespace` option to support `MongoClient.bulk_write`.
366 .. versionchanged:: 3.11
367 Added the ``hint`` option.
368 .. versionchanged:: 3.5
369 Added the ``collation`` option.
370 """
371 if filter is not None:
372 validate_is_mapping("filter", filter)
373 if upsert is not None:
374 validate_boolean("upsert", upsert)
375 if hint is not None and not isinstance(hint, str):

Callers 15

test_bulk_writeMethod · 0.90
test_bulk_writeMethod · 0.90
test_ReplaceOneEqualsMethod · 0.90
setUpMethod · 0.90
setUpMethod · 0.90
asyncSetUpMethod · 0.90
asyncSetUpMethod · 0.90

Calls

no outgoing calls

Tested by 15

test_bulk_writeMethod · 0.72
test_bulk_writeMethod · 0.72
test_ReplaceOneEqualsMethod · 0.72
setUpMethod · 0.72
setUpMethod · 0.72
asyncSetUpMethod · 0.72
asyncSetUpMethod · 0.72