Docs Menu

MongoDB 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
  • Realm Apps
  • Realm Sync
  • App Services
  • Get Started
  • Advanced Guides
  • Realm Database
  • Realm Apps

The MongoDB Realm Node.js SDK allows you to use Realm Database and backend Realm 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.

With the MongoDB 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'");
// 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.

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

Automatically sync data between realms on client devices and your backend MongoDB Atlas data store with Realm 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
See also:

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

To start using the MongoDB 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 Realm 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.

Give Feedback
MongoDB logo
© 2021 MongoDB, Inc.


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