Docs Menu

Relationships & Embedded Objects - Node.js SDK

On this page

  • Relationships
  • To-One Relationship
  • To-Many Relationship
  • Inverse Relationship
  • Embedded Objects

MongoDB Realm allows you to define explicit relationships between the types of objects in a Realm app. A relationship is an object property that references another Realm object type instead of a property type. You define relationships by setting the type of an object property to another object type in the property schema.

Relationships are direct references to other objects in a realm, which means that you don't need bridge tables or explicit joins to define a relationship like you would in a relational database. Instead you can access related objects by reading and writing to the property that defines the relationship. Realm Database executes read operations lazily as they come in, so querying a relationship is just as performant as reading a regular property.

There are three primary types of relationships between objects:

Note
Realm vs Other Databases

Many databases copy objects from database storage into application memory when you need to work with them directly. Objects often contain direct references to other objects, which leaves the developer with a choice of what to copy into memory:

  • You can copy the entire referenced object ahead of time. This means that all referenced data is always available quickly but can use up a lot of resources for objects that are never accessed. Depending on the amount of available memory this may not be viable.
  • You can copy only a foreign key value for each object ahead of time that you can use to query the full object if it's needed. These referenced lookups are memory-efficient but they require additional query code and too many lookups can slow your application down.

Realm's zero-copy query architecture avoids this tradeoff between memory footprint and runtime computational overhead. Instead, Realm queries can directly reference related objects and their properties on disk.

A to-one relationship means that an object is related in a specific way to no more than one other object. You define a to-one relationship for an object type in its object schema by specifying a property where the type is the related Realm object type.

Example

An application could use the following object schemas to indicate that a Person may or may not own a single Dog by including it in its dog property:

A to-many relationship means that an object is related in a specific way to multiple objects. You define a to-many relationship for an object type in its object schema by specifying a property where the type is a list or array of the related Realm object type.

Example

An application could use the following object schemas to indicate that a Person may own multiple Dogs by including them in its dog property:

An inverse relationship links an object back to any other objects that refer to it in a defined to-one or to-many relationship. Relationship definitions are unidirectional, so you must explicitly define a property in the object's model as an inverse relationship.

For example, the to-many relationship "User has many Tasks" does not automatically create the inverse relationship "Task belongs to User". If you don't specify the inverse relationship in the object model, you would need to run a separate query to look up the user that is assigned to a given task.

To define an inverse relationship, define a linkingObjects property in your object model. The linkingObjects definition specifies the object type and property name of the relationship that it inverts.

You cannot manually set the value of an inverse relationship property. Realm Database automatically updates implicit relationships whenever an object is added or removed in the specified relationship.

Example

An application could use the following object schemas to indicate that a User may be assigned many Tasks and that each Task should automatically keep track of which User it's assigned to.

  • The User object's tasks property is defined as a to-many relationship with Task objects and contains all of a given user's assigned tasks.
  • The Task object's assignee property inverts the relationship and automatically updates to refer back to any User object that contains the Task in its tasks property.

An embedded object is a special type of Realm object that models complex data about a specific object. Embedded objects are similar to relationships, but they provide additional constraints and map more naturally to the denormalized MongoDB document model.

Learn more about Embedded objects, including how to read and write embedded objects.

Give Feedback
© 2021 MongoDB, Inc.

About

  • Careers
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2021 MongoDB, Inc.