Docs Menu

Schemas

A schema is a JSON object that defines the the structure and contents of your data. You can use Realm's BSON schemas, which extend the JSON Schema standard, to define your application's data model and validate documents whenever they're created, changed, or deleted.

Schemas represent types of data rather than specific values. Realm supports many built-in schema types. These include primitives, like strings and numbers, as well as structural types, like objects and arrays, which you can combine to create schemas that represent custom object types.

For example, this is a basic schema for data about cars and some car objects that conform to the schema:

Schemas are the specification for your application's data model. Once you've defined a schema, Realm provides you with additional tools and services to work with data that conforms to the schema.

Realm uses schemas in many application services:

  • Realm Sync uses schemas to sync data between realms and MongoDB Atlas. Realm can also generate idiomatic SDK object models for you based on your schemas.
  • The GraphQL API uses schemas to automatically generate a GraphQL schema including types, queries, and mutations. You can extend your app's API with custom resolvers that reference the types defined by your schemas.
  • Data Access Rules validate that data conforms to your schema before and after every request. If any document fails validation, Realm prevents or rolls back the entire request.

A root-level collection schema can contain additional schemas that describe the type's properties. Each root-level schema is an object schema that has the following form:

{
"bsonType": "object",
"title": "<Type Name>",
"required": ["<Required Field Name>", ...],
"properties": {
"<Field Name>": <Schema>
}
}

You can use any of the supported schema types to configure the object's properties:

Note

To learn how to configure and deploy a schema in your app, see Enforce a Schema.

MongoDB Realm validates all write operations (inserts, updates, and deletes) on a MongoDB collection against its collection schema. It checks every document before and after every request to ensure that all properties conform to the schema and that no invalid changes occured.

Realm evaluates the result of all document writes and compares them against the schema before committing the writes to your cluster. If the result of any write operation in a request does not match the schema, MongoDB Realm returns an error to the user without applying any changes in the request.

Example

A collection has the following schema:

{
"title": "person",
"properties": {
"_id": { "bsonType": "objectId" },
"name": { "bsonType": "string" }
}
}

A user with permission to read and write all fields wants to update the name field of a particular document. They issue the following query:

collection.updateOne(
{ "_id": BSON.ObjectId("5ae782e48f25b9dc5c51c4d0") },
{ "$set": { "name": 42 } }
)

The query attempts to set the value of name to the number 42, but the schema requires the value to be a string. MongoDB Realm will reject this write operation even though the user had permission to update the document because the write result does not conform to the schema.

Give Feedback
MongoDB logo
© 2021 MongoDB, Inc.

About

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