Docs Menu

Write Transactions - Node.js SDK

On this page

  • Transaction Lifecycle

A write transaction is a function that can modify the state of objects in a given realm. Write transactions let you create, modify, or delete Realm objects and can encapsulate multiple operations into a single, idempotent update. In other words a transaction is all or nothing: either all of the operations in the transaction succeed or, if any operation fails, none of the operations in the transaction take effect.


Every write operation must occur in a write transaction.


Write transactions are callback functions that you pass to a realm instance to modify objects in that realm. For examples of specific write operations, see Write Operations.

const realm = await;
realm.write(() => {
const tesla = realm.create("Car", {
make: "Tesla",
model: "Model S",
year: 2020,
miles: 12000
const honda = realm.create("Car", {
make: "Honda",
model: "Civic",
year: 2018,
miles: 30000

A given realm only processes one write transaction at a time. When a realm receives a write transaction, it adds the transaction to a queue and evaluates each transaction individually in the order they arrived.

After processing a transaction, Realm either commits it or cancels it:

  • Realm applies all operations in a committed transaction to the realm. Once applied, Realm automatically updates live queries and calls any notification listeners that refer to created, modified, and deleted objects.


    In synced realms, Realm also queues the change to be sent to MongoDB Realm whenever a network is available.

  • Realm does not apply any operations in a cancelled transaction to the realm. Realm cancels a transaction if any operation in the transaction is invalid or fails.
Give Feedback
MongoDB logo
© 2021 MongoDB, Inc.


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