Docs Menu

Docs HomeRealm

Realm Node.js SDK

On this page

  • Local Realm Database
  • Define an Object Schema
  • Query Realm Database
  • Update Live Objects
  • Watch for Object Updates
  • Always Access the Latest Data
  • Atlas App Services Apps
  • Atlas Device Sync
  • App Services
  • Get Started
  • Advanced Guides
  • Realm Database
  • Realm Apps

The Realm Node.js SDK allows you to use Realm Database and backend Apps from Node.js applications written in JavaScript or TypeScript.


The Node.js SDK does not support JavaScript or TypeScript applications written for web browsers. For that use case, you should consider the Web SDK. For development in React Native, refer to the React Native SDK documentation.

Create an Account

With the Realm Node.js SDK, you can access objects stored in a local instance of Realm Database. With Realm Database, you can:

Define your object schema by creating an object or a class.

import Realm from "realm";
const Cat = {
name: "Cat",
properties: {
_id: "objectId",
name: "string",
age: "int",
type: "string",
// open a local realm with the 'Cat' schema
const realm = await{
schema: [Cat],

Query for stored objects:

// retrieve the set of Cat objects
const cats = realm.objects("Cat");
console.log(`There are ${cats.length} cats`);
// filter for cats that are older than 7
const catsOlderThan7 = cats.filtered("age > 7");
`There are ${catsOlderThan7.length} cats older than 7 years old`
// filter for calico cats
const calicoCats = cats.filtered("type == 'Calico'");
console.log(`There are ${calicoCats.length} Calico cats`);

Update objects in Realm Database by updating field values on an instance of the object within a transaction:

// find a cat named "Clover"
const cloverCat = cats.filtered("name == 'Clover'")[0];
// update the Cat in a write transaction
realm.write(() => {
// update clover's age to 5 years old
cloverCat.age = 5;
}); // when the transaction completes, the cat's age is updated in the database
console.log(`Clover the cat is ${cloverCat.age} years old`);

Receive object updates and notifications automatically when objects stored in Realm Database change:

// insert a cat into the database
let aliceCat;
realm.write(() => {
aliceCat = realm.create("Cat", {
_id: new BSON.ObjectID(),
name: "Alice",
age: 14,
type: "Calico",
// create a listener that logs new changes to the cat
aliceCat.addListener((obj, changes) => {
changes.changedProperties.forEach((changedProperty) => {
`${}'s ${changedProperty} was altered to be ${obj[changedProperty]}`
); // This should log "Alice's age was altered to be 15"
// update the cat
realm.write(() => {
aliceCat.age = 15;

Live objects keep all instances of an object up to date at all times:

// always access the latest data
const realmInstanceA = await{
schema: [Cat],
const realmInstanceB = await{
schema: [Cat],
// get a reference to a single cat object
// stored in the database from each realm instance
const calicoCatFromRealmInstanceA = realm
.filtered("type = 'Calico'")[0];
const calicoCatFromRealmInstanceB = realm
.filtered("type = 'Calico'")[0];
// update cat A's name
realm.write(() => { = "Hestu";
// cat B instance automatically updates with the new name


See also:

To get started with Realm Database, try our Quick Start.

Apps are backends for client applications hosted by MongoDB in the cloud. They provide the ability to synchronize data stored in Realm Database, called Device Sync, as well as a layer of backend functionality collectively called Atlas App Services. The Realm Node.js SDK optionally contains the ability to access these Apps running in the cloud. In addition to local Realm Database in the SDK, Apps provide the following functionality:

Automatically sync data between realms on client devices and your backend MongoDB Atlas data store with Device Sync:

var config = {
schema: [Cat], // predefined schema
sync: {
user: app.currentUser,
partitionValue: "MyPartitionValue",
// Open a realm with a configuration object that has a SyncConfiguration
// A SyncConfiguration requires both a logged-in user and a partition value
let realm = await;
// start a write transaction
let darukCat;
realm.write(() => {
// get a cat from the database to update
darukCat = realm.objects("Cat").filtered("name = 'Daruk")[0];
// change the cat's name = "Daruk Goron";
}); // when the transaction completes, the cat's name is updated in the database
// and synced to the connected Realm App
  • Use Realm's built-in user management to enable user account creation and user authentication across devices.

  • Store data persistently with permissions in your backend App using a Atlas database.

  • Execute logic in your backend App from a client application using Functions.

  • React to events in your backend App using Triggers.


See also:

To get started with Realm, try the Quick Start with Sync.

Create an Account

To start using the Realm Node.js SDK in your Node.js application, see Install Realm for Node.js to add the Realm JS SDK dependency and then check out the Quick Start.

To learn more about the concepts that underly Realm Database, such as write transactions, the Query Engine, and more, check out the Fundamentals.

For practical code samples of common tasks in Realm Database and Apps, take a look at the Examples.

Get Started
Realm Apps Examples

For information about advanced concepts, such as encryption and the Query Engine, see the Advanced Guides section.

←  Troubleshooting - .NET SDKInstall Realm for Node.js →
Give Feedback
© 2022 MongoDB, Inc.


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