replaceOne() - Replace a single JSON document within the collection command

Replaces a single JSON document within the collection based on the filter.

Syntax diagram

Read syntax diagramSkip visual syntax diagramdb.collection. replaceOne(<filter>,<replacement><upsert>)

Command parameters

filter
Specifies a filter to select a subset of documents. The command can return an empty result set.
replacement
The replacement document cannot contain update operators..
upsert
  • Optional
  • Possible values: true | false
  • Default value: false
  • If the value is true and no document matches the filter, this optional parameter inserts the document from the replacement parameter. If the value is false, the parameter replaces the document that matches the filter with the replacement document.

Examples

The following example shows the command syntax for replacing a single document in the collection books:
db.books.replaceOne({isbn: "123-456-789"},{ isbn: "123-456-789", author: "Tim, Jules", title: "Our Planet"})
The following example shows the output returned by the previous example:
Updated 1 rows.