Docs HomeMongoDB Manual

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

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 Introduction.要了解MongoDB灵活的模式模型,请参阅数据建模简介