Docs Menu

Realms - .NET SDK

On this page

  • Comparison with Other Databases
  • Key Concepts
  • Synced Realms
  • Realm Schema
  • Realm Files
  • Deleting a Realm File
  • Summary

A realm is a set of related objects that conform to a pre-defined schema. Realms may contain more than one type of data as long as a schema exists for each type.

Every realm stores data in a separate realm file that contains a binary encoding of each object in the realm. You can automatically synchronize a realm across multiple devices and set up reactive event handlers that call a function any time an object in a realm is created, modified, or deleted.

The Realm data model is similar to both relational and document databases but has distinct differences from both. To underscore these differences, it's helpful to highlight what a realm is not:

A realm is not a single, application-wide database.
Unlike other applications, which store all of their data in a single database, Apps often split data across multiple realms to organize data more efficiently and to enforce access controls.
A realm is not a table.
Tables typically only store one kind of information, such as street addresses or items in a store inventory, whereas a realm can contain any number of object types.
A realm is not a collection of schemaless documents.
Application objects are similar to documents, but every object in a realm conforms to a defined schema for its object type. An object cannot contain a field that is not described by its schema.

When using Atlas Device Sync, each realm corresponds to a subset of the data in your App's synced MongoDB Atlas cluster.

When you use Partition-Based Sync, synced realms represent partitions of Atlas data. You can customize the partitioning of data using your application's partition key. Unique values of the partition key, known as partition values, correspond to individual realms.

When you use Flexible Sync, you can customize the data your client application syncs by subscribing to queries. These queries search for data in your App backend, and the Flexible Sync realm syncs data that matches the queries. The client application can only sync data where the user has the appropriate read or read and write permissions to access the data.

You can customize permissions for the data that synced realms can read and write from your App when you configure Realm Rules.

A Realm Schema is a list of valid object schemas that each define an object type that an App may persist. All objects in a realm must conform to the Realm Schema.

Client applications provide a Realm Object Model when they open a realm. If a realm already contains data, then it already has a schema, and when it is opened, Realm Database validates the schema on the client against the existing schema.

To define the schema, you can specify the Schema property on the RealmConfiguration. If you omit this property, then all classes that inherit from RealmObject or EmbeddedObject are added to the schema automatically.

Realm Database stores a binary encoded version of every object and type in a realm in a single .realm file. The file is located at the path that you define in the RealmConfiguration object when you open the realm, or in the default path if you do not specify a path.

The default realm file is named default.realm and is located where the OS stores app-specific data.

Implement Compacting in Your Production Application

Every production application should implement ShouldCompactOnLaunch to periodically reduce the realm file size. For more information about compacting a realm, see: Compact a Realm - .NET SDK.

Auxiliary Realm Files

Realm Database creates additional files for each realm. To learn more about these files, see Realm Database Internals.

In some circumstances, such as a client reset scenario, you might need to delete a realm file and its auxiliary files. If you are developing or debugging the app, you might manually delete the realm file, but doing so when the app is running and any realm instances are still open can cause data corruption.

To delete a realm file while the app is running, you can use the DeleteRealm(configuration) method to safely do so. The following code demonstrates this:

var config = new RealmConfiguration("FileWeThrowAway.realm");
var freshRealm = Realm.GetInstance(config);
Use Caution When Deleting Realm Files

If you delete a realm file or any of its auxiliary files while one or more instances of the realm are open, you might corrupt the realm or disrupt sync.

You may delete these files when all instances of a realm are closed. Before you delete a realm file, however, be sure that you back up any important objects as you will lose all unsynced data in the realm.

  • A realm is a collection of objects that conform to a schema. It is not a single, application-wide database. There can and usually will be more than one realm per application.
  • When using Partition-Based Sync, a partition value is a realm's unique identifier.
  • Realm Rules define who can read or write the realm and apply to the realm as a whole.
  • A Realm Schema is a specification of the object types used in an App.
←  Realm Database - .NET SDKLive Queries - .NET SDK →
Give Feedback
© 2022 MongoDB, Inc.


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