Database Manual / Data Modeling

Schema Validation模式验证

Schema validation lets you create validation rules for your fields, such as allowed data types and value ranges.模式验证允许您为字段创建验证规则,例如允许的数据类型和值范围。

MongoDB uses a flexible schema model, which means that documents in a collection do not need to have the same fields or data types by default. Once you've established an application schema, you can use schema validation to ensure there are no unintended schema changes or improper data types.MongoDB使用灵活的模式模型,这意味着默认情况下集合中的文档不需要具有相同的字段或数据类型。一旦建立了应用程序模式,就可以使用模式验证来确保没有意外的模式更改或不正确的数据类型。

You can implement schema validation in the UI for deployments hosted in MongoDB Atlas.您可以在MongoDB Atlas中托管的部署的UI中实现模式验证

When to Use Schema Validation何时使用架构验证

Your schema validation needs depend on how users use your application. When your application is in the early stages of development, schema validation may impose unhelpful restrictions because you don't know how you want to organize your data. Specifically, the fields in your collections may change over time.模式验证需求取决于用户如何使用应用程序。当应用程序处于开发的早期阶段时,模式验证可能会施加无益的限制,因为您不知道如何组织数据。具体来说,集合中的字段可能会随着时间的推移而变化。

Schema validation is most useful for an established application where you have a good sense of how to organize your data. You can use schema validation in the following scenarios:模式验证对于一个已经建立的应用程序最有用,在这个应用程序中,您对如何组织数据有很好的了解。您可以在以下场景中使用模式验证:

  • For a users collection, ensure that the password field is only stored as a string. This validation prevents users from saving their password as an unexpected data type, like an image.对于用户集合,请确保password字段仅存储为字符串。此验证可防止用户将密码保存为意外的数据类型,如图像。
  • For a sales collection, ensure that the item field belongs to a list of items that your store sells. This validation prevents a user from accidentally misspelling an item name when entering sales data.对于sales集合,请确保item字段属于商店销售的项目列表。此验证可防止用户在输入销售数据时意外拼错商品名称。
  • For a students collection, ensure that the gpa field is always a positive number. This validation prevents errors during data entry.对于学生集合,确保gpa字段始终为正数。此验证可防止数据输入过程中出现错误。

When MongoDB Checks Validation当MongoDB检查验证时

After you add schema validation rules to a collection:将架构验证规则添加到集合后:

  • All document inserts must match the rules.所有文档插入都必须符合规则。
  • The schema validation level defines how the rules are applied to existing documents and document updates. To learn more, see Specify Validation Level for Existing Documents.模式验证级别定义了如何将规则应用于现有文档和文档更新。要了解更多信息,请参阅为现有文档指定验证级别

To find documents in a collection that don't match the schema validation rules, see Find Documents that Don't Match the Schema.要查找集合中不符合架构验证规则的文档,请参阅查找不符合架构的文档

What Happens When a Document Fails Validation文档验证失败时会发生什么

By default, when an insert or update operation would result in an invalid document, MongoDB rejects the operation and does not write the document to the collection.默认情况下,当插入或更新操作会导致无效文档时,MongoDB会拒绝该操作,并且不会将文档写入集合。

Alternatively, you can configure MongoDB to allow invalid documents and log warnings when schema violations occur.或者,您可以将MongoDB配置为在发生模式冲突时允许无效文档和日志警告。

To learn more, see Choose How to Handle Invalid Documents.要了解更多信息,请参阅选择如何处理无效文档

Get Started开始使用

For common tasks involving schema validation, see the following pages:有关涉及模式验证的常见任务,请参阅以下页面:

Learn More了解更多

To learn about MongoDB's flexible schema model, see Data Modeling.要了解MongoDB的灵活模式模型,请参阅数据建模