Docs HomeMongoDB Manual

Model Data for Schema Versioning用于架构版本控制的模型数据

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或不使用它来有条件地处理文档。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意味着应用程序可以识别为新模式成形的文档,并相应地处理它们。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字段,则应用程序将检查架构版本。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.相对于传统的表格数据库,模式版本控制模式可以帮助您更好地决定何时以及如何进行数据迁移。