Navigation

User Objects

MongoDB Realm represents each application user internally with a User Object that includes a unique ID and additional metadata that describes the user. You can access user objects in the following ways:

User objects have the following form:

{
"id": "<Unique User ID>",
"type": "<User Type>",
"data": {
"<Metadata Field>": <Value>,
...
},
"custom_data": {
"<Custom Data Field>": <Value>,
...
},
"identities": [
{
"id": <Unique Identity ID>,
"provider_type": "<Authentication Provider>",
"data": {
"<Metadata Field>": <Value>,
...
}
}
]
}
FieldTypeDescription
idstringA string representation of the ObjectId that uniquely identifies the user.
typestring

The type of the user. The following types are possible:

TypeDescription
"normal"The user is an application user logged in through an authentication provider other than the API Key provider.
"server"The user is a server process logged in with any type of Realm API Key.
"system"The user is the system user that bypasses all rules.
datadocument

A document that contains metadata that describes the user. This field combines the data for all identities associated with the user, so the exact field names and values depend on which authentication providers the user has authenticated with.

Info With Circle IconCreated with Sketch.Note
System Functions Have No User Data

In system functions, the user.data object is empty. Use context.runningAsSystem() to test if the function is running as a system user.

custom_datadocument

A document from your application's custom user data collection that specifies the user's ID. You can use the customer user data collection to store arbitrary data about your application's users. If you set the name field, Realm populates the username metadata field with the return value of name. Realm automatically fetches a new copy of the data whenever a user refreshes their access token, such as when they log in. The underlying data is a regular MongoDB document, so you can use standard CRUD operations through the MongoDB Atlas service to define and modify the user's custom data.

Info With Circle IconCreated with Sketch.Note
Avoid Storing Large Custom User Data

Custom user data is limited to 16MB, the maximum size of a MongoDB document. To avoid hitting this limit, consider storing small and relatively static user data in each custom user data document, such as the user's preferred language or the URL of their avatar image. For data that is large, unbounded, or frequently updated, consider only storing a reference to the data in the custom user document or storing the data with a reference to the user's ID rather than in the custom user document.

identitiesarray

A list of authentication provider identities associated with the user. When a user first logs in with a specific provider, Realm associates the user with an identity object that contains a unique identifier and additional metadata about the user from the provider. For subsequent logins, Realm refreshes the existing identity data but does not create a new identity. Identity objects have the following form:

{
"id": "<Unique ID>",
"provider_type": "<Provider Name>",
"data": {
"<Metadata Field>": <Value>,
...
}
}
Field NameDescription
idA provider-generated string that uniquely identifies this identity
provider_typeThe type of authentication provider associated with this identity.
dataAdditional metadata from the authentication provider that describes the user. The exact field names and values will vary depending on which authentication providers the user has logged in with. For a provider-specific breakdown of user identity data, see User Metadata.
Info With Circle IconCreated with Sketch.Note

In general, MongoDB Realm creates a user object for a given user the first time that they authenticate. If you create a test Email/Password user through the Realm UI, Realm creates that user's user object immediately.

  • The user object contains relevant information about the user that you can use in your app logic.
  • The exact information contained in the user object depends on the authentication providers used.
Give Feedback