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

Method replace_one

pymongo/asynchronous/collection.py:1125–1232  ·  view source on GitHub ↗

Replace a single document matching the filter. >>> async for doc in db.test.find({}): ... print(doc) ... {'x': 1, '_id': ObjectId('54f4c5befba5220aa4d6dee7')} >>> result = await db.test.replace_one({'x': 1}, {'y': 1}) >>> result.matche

(
        self,
        filter: Mapping[str, Any],
        replacement: Mapping[str, Any],
        upsert: bool = False,
        bypass_document_validation: Optional[bool] = None,
        collation: Optional[_CollationIn] = None,
        hint: Optional[_IndexKeyHint] = None,
        session: Optional[AsyncClientSession] = None,
        let: Optional[Mapping[str, Any]] = None,
        sort: Optional[Mapping[str, Any]] = None,
        comment: Optional[Any] = None,
    )

Source from the content-addressed store, hash-verified

1123 )
1124
1125 async def replace_one(
1126 self,
1127 filter: Mapping[str, Any],
1128 replacement: Mapping[str, Any],
1129 upsert: bool = False,
1130 bypass_document_validation: Optional[bool] = None,
1131 collation: Optional[_CollationIn] = None,
1132 hint: Optional[_IndexKeyHint] = None,
1133 session: Optional[AsyncClientSession] = None,
1134 let: Optional[Mapping[str, Any]] = None,
1135 sort: Optional[Mapping[str, Any]] = None,
1136 comment: Optional[Any] = None,
1137 ) -> UpdateResult:
1138 """Replace a single document matching the filter.
1139
1140 >>> async for doc in db.test.find({}):
1141 ... print(doc)
1142 ...
1143 {'x': 1, '_id': ObjectId('54f4c5befba5220aa4d6dee7')}
1144 >>> result = await db.test.replace_one({'x': 1}, {'y': 1})
1145 >>> result.matched_count
1146 1
1147 >>> result.modified_count
1148 1
1149 >>> async for doc in db.test.find({}):
1150 ... print(doc)
1151 ...
1152 {'y': 1, '_id': ObjectId('54f4c5befba5220aa4d6dee7')}
1153
1154 The *upsert* option can be used to insert a new document if a matching
1155 document does not exist.
1156
1157 >>> result = await db.test.replace_one({'x': 1}, {'x': 1}, True)
1158 >>> result.matched_count
1159 0
1160 >>> result.modified_count
1161 0
1162 >>> result.upserted_id
1163 ObjectId('54f11e5c8891e756a6e1abd4')
1164 >>> await db.test.find_one({'x': 1})
1165 {'x': 1, '_id': ObjectId('54f11e5c8891e756a6e1abd4')}
1166
1167 :param filter: A query that matches the document to replace.
1168 :param replacement: The new document.
1169 :param upsert: If ``True``, perform an insert if no documents
1170 match the filter.
1171 :param bypass_document_validation: (optional) If ``True``, allows the
1172 write to opt-out of document level validation. Default is
1173 ``False``.
1174 :param collation: An instance of
1175 :class:`~pymongo.collation.Collation`.
1176 :param hint: An index to use to support the query
1177 predicate specified either by its string name, or in the same
1178 format as passed to
1179 :meth:`~pymongo.asynchronous.collection.AsyncCollection.create_index` (e.g.
1180 ``[('field', ASCENDING)]``). This option is only supported on
1181 MongoDB 4.2 and above.
1182 :param session: a

Callers

nothing calls this directly

Calls 3

_update_retryableMethod · 0.95
UpdateResultClass · 0.90
_write_concern_forMethod · 0.80

Tested by

no test coverage detected