Docs Menu

Custom User Data - .NET SDK

On this page

  • Overview
  • Read a User's Custom User Data
  • Create a User's Custom User Data Document
  • Update a User's Custom Data

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.


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.

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
// and use the gerneic overload of GetCustomData<>()
var cud = user.GetCustomData<CustomUserData>();
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 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 _partition { get; private set; }
public string FavoriteColor { get; set; }
public string LocalTimeZone { get; set; }
public bool IsCool { get; set; }
public CustomUserData(string id, string partition = "myPart")
this._id = id;
this._partition = partition;

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 = user.GetCustomData<CustomUserData>();
Console.WriteLine($"User is cool: {cud.IsCool}");
Console.WriteLine($"User's favorite color is {cud.FavoriteColor}");
Console.WriteLine($"User's timezone is {cud.LocalTimeZone}");
←  Multi-User Applications - .NET SDKClient Resets - .NET SDK →
Give Feedback
© 2022 MongoDB, Inc.


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