VOOZH about

URL: https://www.geeksforgeeks.org/python/python-mongodb-find_one_and_replace-query/

⇱ Python MongoDB - find_one_and_replace Query - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Python MongoDB - find_one_and_replace Query

Last Updated : 15 Jul, 2025

find_one_and_replace() method search one document if finds then replaces with the given second parameter in MongoDb. find_one_and_replace() method is differ from find_one_and_update() with the help of filter it replace the document rather than update the existing document.

Syntax: 

find_one_and_replace(filter, replacement, projection=None, sort=None, return_document=ReturnDocument.BEFORE, session=None, **kwargs)

Parameters 
filter: A query for replacement of a matched document. 

  • replacement: replacement document. 
  • projection: it is optional.A list of a field that should be returned in the result. 
  • sort: key, direction pair for the sort order of query. 
  • return_document: ReturnDocument.BEFORE (default) will return the original document without replacement. ReturnDocument.AFTER will return the replaced or inserted document.
    **kwargs: Additional commands. 

Sample database used in all the below examples:

👁 Image

Example 1: 

Output:

👁 Image
  

Example 2: 

Output: 

👁 Image
 

Comment
Article Tags: