Docs Menu

MongoDB Realm React Native SDK

The MongoDB Realm React Native SDK allows you to use Realm Database and backend Realm apps from React Native applications for iOS and Android written in JavaScript or TypeScript.

Note

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

With the MongoDB Realm React Native 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 Realm.open({
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");
console.log(
`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) => {
console.log(
`${obj.name}'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 Realm.open({
schema: [Cat],
});
const realmInstanceB = await Realm.open({
schema: [Cat],
});
// get a reference to a single cat object
// stored in the database from each realm instance
const calicoCatFromRealmInstanceA = realm
.objects("Cat")
.filtered("type = 'Calico'")[0];
const calicoCatFromRealmInstanceB = realm
.objects("Cat")
.filtered("type = 'Calico'")[0];
// update cat A's name
realm.write(() => {
calicoCatFromRealmInstanceA.name = "Hestu";
});
// cat B instance automatically updates with the new name
expect(calicoCatFromRealmInstanceB.name).toBe("Hestu");
Tip
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 React Native 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 Realm.open(config);
// 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
darukCat.name = "Daruk Goron";
}); // when the transaction completes, the cat's name is updated in the database
// and synced to the connected Realm App
Tip
See also:

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

To start using the MongoDB Realm React Native SDK in your React Native application, see Install Realm for React Native 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
Fundamentals
Examples
Realm Apps Examples

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

Give Feedback
© 2021 MongoDB, Inc.

About

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