| 
 |   | 
 read  source  history  
 
findOneAndUpdate()
 
MongoDB Driver Function Reference 
 
 
Updates a single document based on the specified filter returning the replaced document. 
More information can be found in the corresponding entry of the MongoDB Manual. 
 
 
 
<collection>.findOneAndUpdate(<filter>, <update>[, <options>]) 
 
 |   
 
 
Arguments 
 
 
|  <collection>  |   |  The target collection.  |  
|  <filter>  |   |  A filter expression using query operators.  |  
|  <replacement>  |   |  A BSON document containing updates.  |  
|  <options>  |   |  A BOSN document specifying options.  |  
 
 
 |   
 
 
Options 
 
 
{ arrayfilters: <document>, projection: <document>, sort: <document>, upsert: <boolean>, maxtime: <long>, 
  collation: <document>, hint: <document> } 
 
|  arrayFilters  |   |  An array of filter documents that determine which array elements to modify for an update operation on an array field.  |  
|  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 collation.  |  
|  hint  |   |  A document specifying a hint.  |  
 
 
 |   
 
 
Example 
 
 
db.employees.findOneAndUpdate({ salary: { $gt: 700000 }}, { $set: { bonus: 50000 }}, { sort: { age: 1 }}); 
 
 |   
 
Finds documents from the employee collection with a salary greater than 700000, sorts them by age, then updates the first document in the result. 
 
 
 
 
home | MongoDB Driver Function Reference | MongoDB Driver 
 
		 |