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_version2,以指示它们遵循您的模式的第二次迭代。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. Use the latest schema to store new information in the database.您的应用程序代码可以使用文档的schema_version(或缺少它)有条件地处理文档。使用最新架构在数据库中存储新信息。

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意味着应用程序可以识别为新模式成形的文档并相应地处理它们。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字段。

  • If it does not have a 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字段。
  • If it does have a schema_version field, the application checks the schema version. 如果它确实有schema_version字段,应用程序将检查schema版本。In this example, the 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_version2,应用程序将返回的文档传递给一个专用函数,该函数呈现新的contact_method.workcontact_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字段,应用程序代码可以通过向代码添加专用处理程序函数来支持同一集合中的任意数量的模式迭代。

Use Cases用例

The Schema Versioning pattern is ideal for any one or a combination of the following cases:架构版本控制模式适用于以下任何一种或多种情况:

  • Application downtime is not an option应用程序停机不是一个选项
  • Updating documents may take hours, days, or weeks of time to complete更新文档可能需要数小时、数天或数周的时间才能完成
  • Updating documents to the new schema version is not a requirement不要求将文档更新为新的架构版本

The Schema Versioning pattern helps you better decide when and how data migrations will take place relative to traditional, tabular databases.模式版本控制模式可以帮助您更好地确定数据迁移何时以及如何相对于传统的表格数据库进行。

←  Model Data to Support Keyword SearchModel Monetary Data →