Docs Menu

Docs HomeRealm

Configure, Open, and Close a Realm - Node.js SDK

On this page

  • Open a Realm
  • Open a Realm Without Providing a Schema
  • Open an In-Memory Realm
  • Copy Data and Open a New Realm
  • Close a Realm

To open a local (non-synced) realm, pass a Realm.Configuration object to the asynchronous method


Accessing the Default Realm Path

If you do not specify a path property in your Configuration object, the SDK creates a realm at the default path. You can access and change the default realm path using the Realm.defaultPath global property.

// Open a local realm file with a predefined Car object model
const realm = await{
schema: [Car],

After you create a realm on a device, you can omit the schema when you access the same realm by calling new Realm(). The SDK derives the realm's schema from the existing realm file at Realm.defaultPath.

Accessing a realm without providing a schema only works for local realms. You must always pass a schema when using a Synced realm.

// Open the Realm with a schema
const realm = new Realm({ schema: [Car] });
// Reopen it without a schema
const reopenedRealm = new Realm();

To create a realm that runs entirely in memory without being written to a file, add inMemory: true to your Realm.Configuration object:

const realm = await{
inMemory: true,
schema: [Car],


In-memory realms may use disk space if memory is running low, but files created by an in-memory realm are deleted when you close the realm.

New in version 10.14.0.

To copy data from an existing realm to a new realm with different configuration options, pass the new configuration the Realm.writeCopyTo() method.

In the new realm's configuration, you must specify the path.

If you write the copied realm to a realm file that already exists, the data is written object by object. The copy operation replaces objects if there already exists objects for given primary keys. The schemas of the realm you copy and the realm you are writing to must be compatible for the copy operation to succeed. Only objects in the schemas of both configurations are copied over.

The configuration change can include modifications to SyncConfiguration:

  • Local realm to synced realm

  • Synced Realm to local realm

  • Synced Realm to a Synced Realm for a different user


Partition-Based Sync Only

This method only supports converting between a non-sync realm and Partition-Based Sync. If your app uses Flexible Sync, you must manually iterate through the objects in one realm and copy them into the other realm.


Same-Type Sync Only

This method only supports copying a Partition-Based Sync configuration for another Partition-Based Sync user, or a Flexible Sync configuration for another Flexible Sync user. You cannot use this method to convert between a Partition-Based Sync realm and a Flexible Sync realm or vice-versa.

The configuration change can also include changes to encryptionKey property of the Configuration:

  • Encrypted realm to unencrypted realm

  • Unencrypted realm to encrypted realm


Convert Local Realm to Synced Realm

You can also combine changes to configuration. For example, you can open a local encrypted realm as a synced unencrypted realm or a unencrypted synced realm as an encrypted synced realm.


Convert Synced Encrypted to Local Unencrypted Realm


It is important to remember to call the close() method when done with a realm instance to avoid memory leaks.

←  Work with Realm Files - Node.js SDKReduce Realm File Size - Node.js SDK →
Share Feedback
© 2023 MongoDB, Inc.


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