How do i specify a schema in mongodb?

Docs HomeAtlas App Services

A schema is a JSON object that defines the the structure and contents of your data. You can use Atlas App Services' 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. App Services 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, App Services provides you with additional tools and services to work with data that conforms to the schema.

App Services uses schemas in many application services:

  • Atlas Device Sync uses schemas to sync data between realms and MongoDB Atlas. App Services 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, App Services 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:

  • Object

  • Array

  • String

  • Number

  • Boolean

  • UUID

  • ObjectId

  • Binary Data

  • Mixed

  • Set

  • Dictionary

Note

App Services 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.

App Services 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, App Services 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. App Services 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.

Can we define schema in MongoDB?

Basically, MongoDB is schema-less database, we cannot create schema in MongoDB, but we enforce the collection documents in application code or using MongoDB atlas GUI tool. For generating schema first we need to connect to the specified database and collections in MongoDB.

How do you define a schema?

In computer programming, a schema (pronounced SKEE-mah) is the organization or structure for a database, while in artificial intelligence (AI) a schema is a formal expression of an inference rule. For the former, the activity of data modeling leads to a schema.

How do I get MongoDB schema?

We can get the schema object/first document of the collection using : var schemaObj = db. users. findOne();

How does MongoDB define database schema?

A schema is a JSON object that defines the the structure and contents of your data. You can use Atlas App Services' 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.

Tải thêm tài liệu liên quan đến bài viết How do i specify a schema in mongodb?