Navigation

Quick Start with Sync - Node.js SDK

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

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

To use MongoDB Realm features such as authentication and sync, you must access your Realm app using your Realm app ID. You can find your Realm app ID in the Realm UI.

If you have not enabled Realm Sync or you enabled Sync with development mode, you can define your object model directly in code.

Note

If you have enabled Sync but turned off Development Mode, you can copy and paste the object model definitions that Realm generated for you from the SDKs tab in the Realm 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 Configure Your 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:

Tip
See also:

Once you have enabled Realm Sync and authenticated a user, you can open a synced realm by calling Realm.open():

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 filter:

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.

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 '75.2.1.110:443' (from '192.168.1.154:60136')
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
Give Feedback

On this page