Navigation

Access Custom User Data

Overview

You can store arbitrary custom user data about your users in realm. For example, you might store a user’s preferred language, date of birth, or local timezone. Before writing and reading this data, you must enable custom user data in the backend. To learn more, see Enable Custom User Data.

Note

To use custom user data, you must first Enable Custom User Data.

To create, update, or delete custom user data, you will need the following information from your custom user data configuration:

You can find this information in the Realm UI. In the left sidebar, click App Users, and then select the Custom User Data tab.

Create Custom User Data

To create custom user data for a user, create a MongoDB document in the custom user data collection. The user ID field of the document must contain the the user’s user ID. The following example uses MongoDB Data Access to insert a document containing the user ID of the currently logged in user and several custom properties into the custom user data collection:

app = App.Create(myRealmAppId);
user = await app.LogInAsync(Credentials.Anonymous());

mongoClient = user.GetMongoClient("mongodb-atlas");
dbTracker = mongoClient.GetDatabase("tracker");
cudCollection = dbTracker.GetCollection<CustomUserData>("user_data");

var cud = new CustomUserData(user.Id)
{
    FavoriteColor = "pink",
    LocalTimeZone = "+8",
    IsCool = true
};

var insertResult = await cudCollection.InsertOneAsync(cud);

You may find it helpful to create a C# class (POCO) that represents the custom user data object. The SDK will serialize/deserialize this class to and from BSON when writing, reading, and updating properties. The example above uses the following class to map the properties:

public class CustomUserData
{
    public string _id { get; private set; }

    public string FavoriteColor { get; set; }

    public string LocalTimeZone { get; set; }

    public bool IsCool { get; set; }

    public CustomUserData(string id)
    {
        this._id = id;
    }
}

Read Custom User Data

You retrieve custom user data by calling the GetCustomData() method on the User object of a logged in user:

await user.RefreshCustomDataAsync();

// Tip: define a class that represents the custom data:
var cud = BsonSerializer.Deserialize<CustomUserData>(user.CustomData);

Console.WriteLine($"User is cool: {cud.IsCool}");
Console.WriteLine($"User's favorite color is {cud.FavoriteColor}");
Console.WriteLine($"User's timezone is {cud.LocalTimeZone}");

Custom Data May Be Stale

MongoDB Realm does not dynamically update the value of the CustomData immediately when underlying data changes. Instead, MongoDB Realm fetches the most recent version of custom user data whenever a user refreshes their access token or when you explicitly call the RefreshCustomDataAsync() method, which ensures your app has the latest customer user data.

Update Custom User Data

Updating custom user data uses the same code as writing. The following example finds and updates the data by using the UpdateOneAsync() method, and then refreshes the data to ensure the latest changes are available:

var updateResult = await cudCollection.UpdateOneAsync(
    new BsonDocument("_id", user.Id),
    new BsonDocument("$set", new BsonDocument("IsCool", false)));

await user.RefreshCustomDataAsync();
var cud = BsonSerializer.Deserialize<CustomUserData>(user.CustomData);

Console.WriteLine($"User is cool: {cud.IsCool}");
Console.WriteLine($"User's favorite color is {cud.FavoriteColor}");
Console.WriteLine($"User's timezone is {cud.LocalTimeZone}");

Summary

  • You can use custom user data to store arbitrary information about your application users.
  • MongoDB Realm stores custom user data as a document in a MongoDB collection determined by your application configuration.
  • MongoDB Realm associates custom user data with a user based on the value of the user ID field.