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

Method update_one

pymongo/synchronous/collection.py:1233–1351  ·  view source on GitHub ↗

Update a single document matching the filter. >>> for doc in db.test.find(): ... print(doc) ... {'x': 1, '_id': 0} {'x': 1, '_id': 1} {'x': 1, '_id': 2} >>> result = db.test.update_one({'x': 1}, {'$inc': {'x': 3}})

(
        self,
        filter: Mapping[str, Any],
        update: Union[Mapping[str, Any], _Pipeline],
        upsert: bool = False,
        bypass_document_validation: Optional[bool] = None,
        collation: Optional[_CollationIn] = None,
        array_filters: Optional[Sequence[Mapping[str, Any]]] = 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

1231 )
1232
1233 def update_one(
1234 self,
1235 filter: Mapping[str, Any],
1236 update: Union[Mapping[str, Any], _Pipeline],
1237 upsert: bool = False,
1238 bypass_document_validation: Optional[bool] = None,
1239 collation: Optional[_CollationIn] = None,
1240 array_filters: Optional[Sequence[Mapping[str, Any]]] = None,
1241 hint: Optional[_IndexKeyHint] = None,
1242 session: Optional[ClientSession] = None,
1243 let: Optional[Mapping[str, Any]] = None,
1244 sort: Optional[Mapping[str, Any]] = None,
1245 comment: Optional[Any] = None,
1246 ) -> UpdateResult:
1247 """Update a single document matching the filter.
1248
1249 >>> for doc in db.test.find():
1250 ... print(doc)
1251 ...
1252 {'x': 1, '_id': 0}
1253 {'x': 1, '_id': 1}
1254 {'x': 1, '_id': 2}
1255 >>> result = db.test.update_one({'x': 1}, {'$inc': {'x': 3}})
1256 >>> result.matched_count
1257 1
1258 >>> result.modified_count
1259 1
1260 >>> for doc in db.test.find():
1261 ... print(doc)
1262 ...
1263 {'x': 4, '_id': 0}
1264 {'x': 1, '_id': 1}
1265 {'x': 1, '_id': 2}
1266
1267 If ``upsert=True`` and no documents match the filter, create a
1268 new document based on the filter criteria and update modifications.
1269
1270 >>> result = db.test.update_one({'x': -10}, {'$inc': {'x': 3}}, upsert=True)
1271 >>> result.matched_count
1272 0
1273 >>> result.modified_count
1274 0
1275 >>> result.upserted_id
1276 ObjectId('626a678eeaa80587d4bb3fb7')
1277 >>> db.test.find_one(result.upserted_id)
1278 {'_id': ObjectId('626a678eeaa80587d4bb3fb7'), 'x': -7}
1279
1280 :param filter: A query that matches the document to update.
1281 :param update: The modifications to apply.
1282 :param upsert: If ``True``, perform an insert if no documents
1283 match the filter.
1284 :param bypass_document_validation: (optional) If ``True``, allows the
1285 write to opt-out of document level validation. Default is
1286 ``False``.
1287 :param collation: An instance of
1288 :class:`~pymongo.collation.Collation`.
1289 :param array_filters: A list of filters specifying which
1290 array elements an update should apply.

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