ReQL command reference - RethinkDB
ReQL command: replace
Command syntax
table.replace(object | function) → object
selection.replace(object | function) → object
singleSelection.replace(object | function) → object

Description
Replace documents in a table. Accepts a JSON document or a ReQL expression, and replaces the original document with the new one. The new document must have the same primary key as the original document.
The replace command can be used to both insert and delete documents. If
the “replaced” document has a primary key that doesn’t exist in the table,
the document will be inserted; if an existing document is replaced with
null, the document will be deleted. Since update and replace operations
are performed atomically, this allows atomic inserts and deletes as well.
You can pass the following options using optArg:
durability: possible values arehardandsoft. This option will override the table or query’s durability setting (set in run). In soft durability mode RethinkDB will acknowledge the write immediately after receiving it, but before the write has been committed to disk.return_changes:true: return achangesarray consisting ofold_val/new_valobjects describing the changes made, only including the documents actually updated.false: do not return achangesarray (the default)."always": behave astrue, but include all documents the command tried to update whether or not the update was successful. (This was the behavior oftruepre-2.0.)
non_atomic: if set totrue, executes the replacement and distributes the result to replicas in a non-atomic fashion. This flag is required to perform non-deterministic updates, such as those that require reading data from another table.ignore_write_hook: Iftrue, and if the user has the config permission, ignores any write hook when performing the replacement.
Replace returns an object that contains the following attributes:
replaced: the number of documents that were replaced.unchanged: the number of documents that would have been modified, except that the new value was the same as the old value.inserted: the number of new documents added. A document is considered inserted if its primary key did not exist in the table at the time of thereplaceoperation.deleted: the number of deleted documents when doing a replace withnull.errors: the number of errors encountered while performing the replace.first_error: If errors were encountered, contains the text of the first error.skipped: 0 for a replace operation.changes: ifreturnChangesis set totrue, this will be an array of objects, one for each objected affected by thereplaceoperation. Each object will have two keys:{"new_val": <new value>, "old_val": <old value>}.
RethinkDB write operations will only throw exceptions if errors occur before any writes. Other errors will be listed in first_error, and errors will be set to a non-zero count. To properly handle errors with this term, code must both handle exceptions and check the errors return value!
Example: Replace the document with the primary key 1.
r.table("posts").get(1).replace(
r.hashMap("id", 1).with("title", "Lorem ipsum")
.with("content", "Aleas jacta est")
.with("status", "draft")
).run(conn);
Example: Remove the field status from all posts.
r.table("posts").replace(post -> post.without("status")).run(conn);
Example: Remove all the fields that are not id, title or content.
r.table("posts").replace(
post -> post.pluck("id", "title", "content")
).run(conn);
Example: Replace the document with the primary key 1 using soft durability.
r.table("posts").get(1).replace(
r.hashMap("id", 1)
.with("title", "Lorem ipsum")
.with("content", "Aleas jacta est")
.with("status", "draft")
).optArg("durability", "soft").run(conn);
Example: Replace the document with the primary key 1 and return the values of the document before
and after the replace operation.
r.table("posts").get(1).replace(
r.hashMap("id", 1)
.with("title", "Lorem ipsum")
.with("content", "Aleas jacta est")
.with("status", "published")
).optArg("return_changes", true).run(conn);
The result will have a changes field:
{
"deleted": 0,
"errors": 0,
"inserted": 0,
"changes": [
{
"new_val": {
"id":1,
"title": "Lorem ipsum"
"content": "Aleas jacta est",
"status": "published",
},
"old_val": {
"id":1,
"title": "Lorem ipsum"
"content": "TODO",
"status": "draft",
"author": "William",
}
}
],
"replaced": 1,
"skipped": 0,
"unchanged": 0
}
Related commands
Get more help
Couldn't find what you were looking for?
- Ask a question on Stack Overflow
- Chat with us and our community on Slack
- Talk to the team on IRC on #rethinkdb@freenode.net — via Webchat
- Ping @rethinkdb on Twitter
- Post an issue on the documentation issue tracker on GitHub