Docs Menu

Docs HomeRealm

Quick Start with Sync - Node.js SDK

On this page

  • Overview
  • Import Realm
  • Initialize the App
  • Define Your Object Model
  • Authenticate a User
  • Open a Realm
  • Create, Read, Update, and Delete Objects
  • Create New Objects
  • Get a Collection of Objects
  • Filter & Query Collections
  • Modify an Object
  • Delete an Object
  • Watch for Changes
  • Log Out
  • Complete Example
  • Code
  • Output

This page contains information to quickly get Realm Database integrated into your app. Before you begin, ensure you have:

  • Created an App

  • Enabled Device Sync

  • Installed the Node SDK

At the top of your source files where you want to use Realm, add the following line to import the SDK.

To use Atlas App Services features such as authentication and sync, you must access your App using your App ID. You can find your App ID in the App Services UI.

If you have not enabled Device Sync or you enabled Sync with Development Mode, you can define your object model directly in code.


Get Schema from UI if Development Mode Disabled

If you have enabled Sync but turned off Development Mode, you can copy and paste the object model definitions that App Services generated for you from the SDKs tab in the App Services UI. You must re-enable Development Mode if you want to make changes to the object model definition from client side code.

For more information, see Create a Data Model.

To authenticate and log in a user, call App.logIn(). When anonymous authentication is enabled, users can immediately log into your app without providing any identifying information:


Once you have enabled Device Sync and authenticated a user, you can open a synced realm by calling

Once you have opened a realm, you can modify it and its objects in a write transaction block.

To create a new object, start a new transaction by calling Realm.write() and then call Realm.create() in the transaction callback function:

You can retrieve a live collection that contains all objects of a given type in the realm:

You can filter the objects in a collection by providing a query:

To modify an object, update its properties in a Realm.write() transaction block:

To delete an object, pass it to Realm.delete() in a Realm.write() transaction block:

You can watch a realm, collection, or object for changes by registering event handlers with the Realm.addListener() Object.addListener() Collection.addListener() methods.


Serverless Limitations

You cannot watch for changes if the data source is an Atlas serverless instance. MongoDB serverless currently does not support change streams, which are used on watched collections to listen for changes.

To remove all registered realm, object, and collection listeners associated with a realm, call Realm.removeAllListeners().

To remove a specific listener, call Object.removeListener() on the object that is being watched with the listener function as the argument.

You can log out an authenticated user by calling User.logOut():

The following code is a fully runnable example that includes all of the concepts on this page. Make sure to replace <Your App ID> with your App ID.

Running the above code should output something like the following:

Logged in anonymously with user id: 5f1f9fb75512f2cb670ad033
Connection[1]: Session[1]: client_reset_config = false, Realm exists = false, async open = false, client reset = false
Connection[1]: Connected to endpoint '' (from '')
created two tasks: go grocery shopping & go exercise
task {
"_id": "5f1f9fb70a74d46af33c7a02",
"_partition": null,
"name": "go grocery shopping",
"status": "Open"
inserted task: "go grocery shopping"
inserted task: "go exercise"
modified task: {
"_id": "5f1f9fb70a74d46af33c7a02",
"_partition": null,
"name": "go grocery shopping",
"status": "InProgress"
Connection[1]: Disconnected
←  Quick Start - Node.js SDKRealm Fundamentals - Node.js SDK →
Give Feedback
© 2022 MongoDB, Inc.


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