On this page本页内容
Database schemas occasionally need to be updated. 数据库模式偶尔需要更新。For example, a schema designed to hold user contact information may need to be updated to include new methods of communication as they become popular, such as Twitter or Skype.例如,设计用于保存用户联系信息的模式可能需要更新,以便在新的通信方法变得流行时包括它们,例如Twitter或Skype。
You can use MongoDB’s flexible schema model, which supports differently shaped documents in the same collection, to gradually update your collection’s schema. 您可以使用MongoDB的灵活模式模型来逐步更新集合的模式,该模型支持同一集合中不同形状的文档。As you update your schema model, the Schema Versioning pattern allows you to track these updates with version numbers. 在更新模式模型时,模式版本控制模式允许您使用版本号跟踪这些更新。Your application code can use version numbers to identify and handle differently shaped documents without downtime.您的应用程序代码可以使用版本号来识别和处理不同形状的文档,而无需停机。
To implement the Schema Versioning pattern, add a 要实现模式版本控制模式,请在第一次修改模式时向模式中添加一个schema_version
(or similarly named) field to your schema the first time that you modify your schema. schema_version
(或类似名称)字段。Documents that use the new schema should have a 使用新模式的文档应该有一个schema_version
of 2
to indicate that they adhere to the second iteration of your schema. schema_version
2
,以表明它们遵循您的模式的第二次迭代。If you update your schema again, increment the 如果再次更新模式,请增加schema_version
.schema_version
。
Your application code can use a document’s 应用程序代码可以使用文档的schema_version
, or lack thereof, to conditionally handle documents. schema_version
,也可以不使用schema_version
,有条件地处理文档。Use the latest schema to store new information in the database.使用最新架构在数据库中存储新信息。
The following example iterates upon the schema for documents in the 下面的示例迭代users
collection.users
集合中文档的模式。
In the first iteration of this schema, a record includes 在该模式的第一次迭代中,记录包括galactic_id
, name
, and phone
fields:galactic_id
、name
和phone
字段:
In the next iteration, the schema is updated to include more information in a different shape:在下一次迭代中,将更新模式,以在不同的形状中包含更多信息:
Adding a 添加schema_version
means that an application can identify documents shaped for the new schema and handle them accordingly. schema_version
意味着应用程序可以识别为新schema生成的文档,并相应地处理它们。The application can still handle old documents if 如果文档上不存在schema_version
does not exist on the document.schema_version
,应用程序仍然可以处理旧文档。
For example, consider an application that finds a user’s phone number(s) by 例如,考虑通过galactic_id
. galactic_id
查找用户的电话号码的应用程序。Upon being given a 当得到一个galactic_id
, the application needs to query the database:galactic_id
时,应用程序需要查询数据库:
After the document is returned from the database, the application checks to see whether the document has a 从数据库返回文档后,应用程序会检查文档是否有schema_version
field.schema_version
字段。
schema_version
field, the application passes the returned document to a dedicated function that renders the phone
field from the original schema.schema_version
字段,则应用程序会将返回的文档传递给一个专用函数,该函数会根据原始架构呈现phone
字段。schema_version
field, the application checks the schema version. schema_version
字段,应用程序将检查schema版本。schema_version
is 2
and the application passes the returned document to a dedicated function that renders the new contact_method.work
and contact_method.home
fields.schema_version
为2
,应用程序将返回的文档传递给一个专用函数,该函数呈现新的contact_method.work
和contact_method.home
字段。Using the 通过使用schema_version
field, application code can support any number of schema iterations in the same collection by adding dedicated handler functions to the code.schema_version
字段,应用程序代码可以通过向代码中添加专用的处理程序函数来支持同一集合中任意数量的模式迭代。
The Schema Versioning pattern is ideal for any one or a combination of the following cases:模式版本控制模式适用于以下任何一种或多种情况:
The Schema Versioning pattern helps you better decide when and how data migrations will take place relative to traditional, tabular databases.模式版本控制模式可以帮助您更好地决定相对于传统的表格数据库,何时以及如何进行数据迁移。