Docs Menu


On this page

  • Definition
  • Usage
  • Example
  • Parameters
  • Return Value

Overwrite a single document in a collection or view based on a query and return the document in either its pre-replacement or post-replacement form. Unlike collection.updateOne(), this action allows you to atomically find, replace, and return a document with the same command. This avoids the risk of other update operations changing the document between separate find and update operations.

To call the collection.findOneAndUpdate() action from a Function, get a collection handle with database.collection() then call the handle's findOneAndUpdate() method.

// Find the document that describes "lego"
const query = { "name": "lego" };
// Replace it with a new document
const replacement = {
"name": "blocks",
"price": 20.99,
"category": "toys"
// Return the original document as it was before being replaced
const options = { "returnNewDocument": false };
return itemsCollection.findOneAndReplace(query, replacement, options)
.then(replacedDocument => {
if(replacedDocument) {
console.log(`Successfully replaced the following document: ${replacedDocument}.`)
} else {
console.log("No document matches the provided query.")
return updatedDocument
.catch(err => console.error(`Failed to find and replace document: ${err}`))

The collection.findOneAndReplace() action has the following form:

findOneAndReplace(query, replacement, options)


query: <document>

Required. A standard MongoDB query document that specifies which document to replace. You can use most query selectors except for evaluation, geospatial, or bitwise selectors.

If multiple documents match the query, only the first document in sort order or natural order will be updated.

Replacement Document

replacement: <document>
Required. The document that should replace the found document. The document cannot contain any MongoDB update operators.

Replacement Options

options: <document>

A document that specifies configuration options for the query. The options document has the following form:

"upsert": <boolean>,
"sort": <document>,
"projection": <document>,
"returnNewDocument": <boolean>


options.upsert: <boolean>
Optional. Default: false. A boolean that, if true, indicates that MongoDB should insert a new document that matches the query when the query does not match any existing documents in the collection.


options.sort: <document>

Optional. Specifies the query sort order. Sort documents specify one or more fields to sort on where the value of each field indicates whether MongoDB should sort it in ascending (1) or descending (-1) order. The sort order determines which document collection.findOneAndReplace() affects.


The following sort document specifies that documents should be sorted first by age from highest to lowest. Once sorted by age, the result set should further be sorted by name in alphabetical order for each distinct age value.

{ age: -1, name: 1 }


options.projection: <document>

Optional. A document that specifies which fields MongoDB should return or withhold in each document that matches the query.

To return all fields in the matching documents, omit this parameter or specify an empty projection document ({}).

To return specific fields and the document's _id, specify the fields in the projection document with a value of 1:

// Includes the field in returned documents
{ <Field Name>: 1 }

To withhold specific fields, specify the fields in the projection document with a value of 0:

// Withholds the field from returned documents
{ <Field Name>: 0 }

You may specify either fields to include or fields to withhold but not both. For example, the following projection is invalid because it simultaneously includes the name field and withholds the address field:

// Invalid
// Can't simultaneously include and withhold
{ "name": 1, "address": 0 }

The exception to this rule is the _id field, which you may withhold from any query:

// Valid
// Can exclude _id while including other fields
{ "_id": 0, "name": 1 }

Return New Document

options.returnNewDocument: <boolean>
Optional. Default: false. A boolean that, if true, indicates that the action should return the document in its post-replace form instead of its original form from before the replace operation.

Client Session

session: <ClientSession>
Optional. A client session that specifies the transaction context in which the operation occurs. To learn more, see Transactions.

You can specify whether to return the pre-replacement or post-replacement version of the document by setting the value of options.returnNewDocument. By default, returnNewDocument is false, which indicates that the promise should resolve to the pre-update version of the document.

The collection.findOneAndReplace() action returns a Promise that resolves to a single document that the query overwrote. If no documents match the specified query, the promise resolves to null.

Give Feedback
MongoDB logo
© 2021 MongoDB, Inc.


  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2021 MongoDB, Inc.