|  |  | read  source  history  
= findOneAndReplace() =
 ----
 [[MongoDB Driver Function Reference]]
 
 
 Replaces a single document based on the specified filter returning the replaced document.
 More information can be found in the corresponding entry of the [[https://docs.mongodb.com/manual/reference/method/db.collection.findOneAndReplace/index.html|MongoDB Manual]].
 
 
 [[[@cG
 
 <collection>.findOneAndReplace(<filter>, <replacement>[, <options>])
 
 ]]]
 
 
 **Arguments**
 
 [[[@w
 
 |@t0@r0@c0 <collection> | | The target collection. |
 | <filter> | | A filter expression using [[https://docs.mongodb.com/manual/reference/operator/|query operators]]. |
 | <replacement> | | A BSON document to replace the document. |
 | <options> | | A BOSN document specifying options. |
 
 ]]]
 
 
 **Options**
 
 [[[@cw
 
 { projection: <document>, sort: <document>, upsert: <boolean>, maxtime: <long>,
 collation: <document>, hint: <document> }
 <<@f:text>>
 |@t0@r0@c0 projection | | Defines a subset of fields to return. |
 | sort | | Specifies a sort order. |
 | upsert | | Toggles an upsert. |
 | maxtime | | Specifies a time limit in milliseconds. |
 | collation| | A document specifying the [[https://docs.mongodb.com/manual/reference/bson-type-comparison-order/#collation|collation]]. |
 | hint | | A document specifying a [[https://docs.mongodb.com/manual/reference/method/db.collection.deleteMany/#ex-deletemany-hint|hint]]. |
 <<@/f>>
 ]]]
 
 
 **Return Value**
 
 [[[@w
 
 Returns a result set (cursor).
 
 ]]]
 
 
 **Example**
 
 [[[@cw
 
 db.employees.findOneAndReplace({ salary: { $gt: 700000 }}, { name: "Roo", firstname: "Kanga", age: 20, salary: 100000 }, { sort: { age: 1 }});
 
 ]]]
 
 Finds documents from the employee collection with a salary greater than 700000, sorts them by age, then replaces the first document in the result.
 
 
 
 ----
 [[Main|home]] | [[MongoDB Driver Function Reference]] | [[MongoDB Driver]]
 
 |