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

Method replace_one

pymongo/synchronous/collection.py:1124–1231  ·  view source on GitHub ↗

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

(
        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[ClientSession] = 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

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

Callers 15

test_updateMethod · 0.45
test_non_bulk_writesMethod · 0.45
callbackMethod · 0.45
test_insert_find_oneMethod · 0.45
test_writesMethod · 0.45

Calls 3

_update_retryableMethod · 0.95
UpdateResultClass · 0.90
_write_concern_forMethod · 0.80

Tested by 15

test_updateMethod · 0.36
test_non_bulk_writesMethod · 0.36
callbackMethod · 0.36
test_insert_find_oneMethod · 0.36
test_writesMethod · 0.36