Docs HomeDevelop ApplicationsMongoDB Manual

Schema Validation架构验证

On this page本页内容

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. MongoDB使用灵活的模式模型,这意味着集合中的文档在默认情况下不需要具有相同的字段或数据类型。Once you've established an application schema, you can use schema validation to ensure there are no unintended schema changes or improper data types.一旦建立了应用程序模式,就可以使用模式验证来确保没有意外的模式更改或不正确的数据类型。

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. 对于用户集合,请确保password字段仅存储为字符串。This validation prevents users from saving their password as an unexpected data type, like an image.此验证可防止用户将密码保存为意外的数据类型,如图像。

  • For a sales collection, ensure that the item field belongs to a list of items that your store sells. 对于销售集合,请确保item字段属于商店销售的项目列表。This validation prevents a user from accidentally misspelling an item name when entering sales data.此验证可防止用户在输入销售数据时意外拼错商品名称。

  • For a students collection, ensure that the gpa field is always a positive number. 对于学生集合,确保gpa字段始终为正数。This validation catches typos during data entry.此验证在数据输入期间捕获错误。

When MongoDB Checks ValidationMongoDB检查验证时

When you create a new collection with schema validation, MongoDB checks validation during updates and inserts in that collection.当您使用模式验证创建新集合时,MongoDB会在更新和插入该集合时检查验证。

When you add validation to an existing, non-empty collection:向现有非空集合添加验证时:

  • Newly inserted documents are checked for validation.检查新插入的文档是否有效。

  • Documents already existing in your collection are not checked for validation until they are modified. 在修改集合中已存在的文档之前,不会对其进行验证检查。Specific behavior for existing documents depends on your chosen validation level. 现有文档的特定行为取决于您选择的验证级别。To learn more, see Specify Validation Level for Existing Documents.要了解更多信息,请参阅为现有文档指定验证级别

Adding validation to an existing collection does not enforce validation on existing documents. 向现有集合添加验证不会对现有文档强制验证。To check a collection for invalid documents, use the validate command.要检查集合中的无效文档,请使用validate命令。

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.要了解更多信息,请参阅选择如何处理无效文档

Tasks任务

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

Learn More

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

←  Data Modeling IntroductionSpecify JSON Schema Validation →