Navigation

Define Custom User Data

On this page

Overview

You can store arbitrary data about your application users in a MongoDB collection and configure MongoDB Realm to automatically expose each user’s data in a field of their user object. For example, you might store a user’s preferred language, date of birth, or their local timezone.

MongoDB Realm automatically finds a user’s custom data document and includes it in their access token when they log in. You can access the data in the custom_data field of the user’s object in function context, the %%user expansion, and their client application access token.

MongoDB Realm does not manage custom user documents so you are responsible for creating and deleting them. The underlying data is a regular MongoDB document, so you can use standard CRUD operations through the MongoDB Atlas service to define and modify a user’s custom data. You can also use authentication triggers to dynamically update user documents, such as storing the time of their most recent login in the lastLogin field.

Example

You can access custom user data in the client SDKs as well as functions and rule expressions.

const user = context.user;
const speaksEnglish = user.custom_data.primaryLanguage === "English";
{
  "%%user.custom_data.primaryLanguage": "English"
}
let app: RealmAppClient = Realm.defaultAppClient
let user: RealmUser = app.auth.currentUser
let speaksEnglish: Bool = user.customData.primaryLanguage == "English"
RealmAppClient app = Realm.getDefaultAppClient();
RealmUser user = client.getAuth().getUser();
Boolean speaksEnglish = user.getCustomData().primaryLanguage == "English";
const app = Realm.defaultAppClient;
const user = app.auth.user;
const speaksEnglish = user.customData.primaryLanguage === "English";

Store One Document Per User

Documents that contain user data must include the user’s ID in a specific field. If multiple documents specify the same user’s ID, MongoDB Realm only exposes the data from the document that was inserted first.

Custom Data May Be Stale

MongoDB Realm does not dynamically update a user’s custom data if the underlying document changes. Instead, MongoDB Realm fetches a new copy of the data whenever a user refreshes their access token, such as when they log in. This may mean that the custom data won’t immediately reflect changes, e.g. updates from an authentication Trigger. The client SDKs automatically refresh a logged-in user’s access token periodically, so the user’s custom data should never remain stale for more than 30 minutes.

Procedure

1
2

Enable Custom User Data

To configure MongoDB Realm to associate data in a collection with your application’s users, set the Enable Custom User Data toggle to On.

3

Specify the Custom User Data Collection

You must store the custom data for your application’s users in a single collection of a linked MongoDB Atlas cluster. To configure your application to read user data from this collection, you need to specify the following values:

  • Cluster Name: The name of a linked MongoDB cluster that contains the custom user data collection.
  • Database Name: The name of the MongoDB database that contains the custom user data collection.
  • Collection Name: The name of the MongoDB collection that contains custom user data.
4

Specify the User ID Field

Every document in the custom user data collection should have a field that contains the user ID of the Realm user that it describes. Specify the name of the field that contains each user’s ID in the User ID Field input.

5

Deploy the Updated Application

Once you have configured the customer user data collection, you can make custom user data available to client applications by deploying your application. To deploy a draft application from the Realm UI:

  1. Click Deploy in the left-hand navigation
  2. Find the draft in the deployment history table and then click Review & Deploy Changes.
  3. Review the diff of changes and then click Deploy.

Once the application successfully deploys, Realm begins to associate custom data with users. When a user logs in, Realm automatically queries the custom user data collection for a document where the specified User ID Field contains the user’s ID. If a document matches, Realm exposes the data in the document in the custom_data field of that user’s user object.

1

Export Your Realm Application

You can set up and enable custom user data through realm-cli or automatic GitHub deployment. To configure custom user data, you need the latest version of the application directory for your application.

You can export your application from the Export tab of the Settings page in the Realm UI, or by running the following command from an authenticated instance of realm-cli:

realm-cli export --app-id=<App ID>
2

Configure Custom User Data

You must store the custom data for your application’s users in a single collection of a linked MongoDB Atlas cluster. Every document in the collection should have a field that contains the user ID of the Realm user that it describes.

To configure your application to read user data from this collection, specify a configuration document in the custom_user_data_config field of realm.json:

realm.json
{
  ...,
  "custom_user_data_config": {
    "enabled": <boolean>,
    "mongo_service_id": "<Linked MongoDB Cluster Service ID>",
    "database_name": "<Database Name>",
    "collection_name": "<Collection Name>",
    "user_id_field": "<User ID Field Name>"
  }
}
Field Description
enabled A boolean that, if true, indicates that Realm should associate data in the specified collection with application users.
mongo_service_id The service ID of the linked MongoDB cluster that contains the custom user data collection.
database_name The name of the MongoDB database that contains the custom user data collection.
collection_name The name of the MongoDB collection that contains custom user data.
user_id_field The name of a field that contains an application user’s ID value. All user documents in the collection must have this field and no two documents may contain the same user ID.
3

Deploy the Updated Application

Once you have configured the customer user data collection, you can make custom user data available to client applications by deploying your application.

To deploy a draft application with realm-cli:

realm-cli import

To deploy a draft application with automatic GitHub deployment:

git add realm.json
git commit -m "Configure and Enable Custom User Data"
git push origin master

Once the application successfully deploys, MongoDB Realm begins to associate custom data with users. When a user logs in, Realm automatically queries the custom user data collection for a document where the specified User ID Field contains the user’s ID. If a document matches, Realm exposes the data in the document in the custom_data field of that user’s user object.