Model Data for Schema Versioning模式版本控制的模型数据

On this page本页内容

Overview概述

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.您的应用程序代码可以使用版本号来识别和处理不同形状的文档,而无需停机。

Schema Versioning Pattern模式版本控制模式

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.使用最新架构在数据库中存储新信息。

Example示例

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_idnamephone字段:

// users collection

{
    "_id": "<ObjectId>",
    "galactic_id": 123,
    "name": "Anakin Skywalker",
    "phone": "503-555-0000",
}

In the next iteration, the schema is updated to include more information in a different shape:在下一次迭代中,将更新模式,以在不同的形状中包含更多信息:

// users collection

{
    "_id": "<ObjectId>",
    "galactic_id": 123,
    "name": "Darth Vader",
    "contact_method": {
        "work": "503-555-0210",
        "home": "503-555-0220",
        "twitter": "@realdarthvader",
        "skype": "AlwaysWithYou"
    },
"schema_version": "2"}

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时,应用程序需要查询数据库:

db.users.find( { galactic_id: 123 } );

After the document is returned from the database, the application checks to see whether the document has a schema_version field.从数据库返回文档后,应用程序会检查文档是否有schema_version字段。

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字段,应用程序代码可以通过向代码中添加专用的处理程序函数来支持同一集合中任意数量的模式迭代。

Use Cases用例

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.模式版本控制模式可以帮助您更好地决定相对于传统的表格数据库,何时以及如何进行数据迁移。