Your application's schema requirements may change over time. For example, when new services become available, you may need to add new fields to your documents. MongoDB's flexible data model means that you can use a non-uniform document structure throughout your collection, and you can retain your older document structure alongside the updated schema.应用程序的架构要求可能会随着时间的推移而变化。例如,当新服务可用时,您可能需要向文档中添加新字段。MongoDB灵活的数据模型意味着您可以在整个集合中使用非一致性的文档结构,并且可以在更新的模式旁边保留旧的文档结构。
The Schema Versioning Pattern lets you have different versions of your schema in the same collection, which avoids large-scale schema migrations when requirements change.模式版本控制模式允许您在同一集合中拥有不同版本的模式,从而避免了在需求发生变化时进行大规模的模式迁移。
About this Task关于此任务
If you have a field that appears at different levels in your documents, the Schema Versioning Pattern may affect your indexes. For example, if you store the same field as both a top-level field and as an embedded field in different documents, you may need multiple indexes to support queries on that field.如果文档中有一个出现在不同级别的字段,则架构版本控制模式可能会影响索引。例如,如果将同一字段作为顶级字段和嵌入字段存储在不同的文档中,则可能需要多个索引来支持对该字段的查询。
Before you Begin开始之前
In the following example, an online store uses a collection to track customer contact information. At first, the collection only contains home and work phone numbers. Over time, new contact methods are added and some older methods aren't needed.在以下示例中,在线商店使用集合来跟踪客户联系信息。起初,该集合只包含家庭和工作电话号码。随着时间的推移,新的联系方式被添加,一些旧的方法不再需要。
Insert the sample document:插入示例文档:
db.contacts.insertOne(
{
_id: 1,
name: "Taylor",
home: "209-555-7788",
work: "503-555-0110"
}
)Steps步骤
The following procedure sets the initial schema version for the collection, then inserts a new document with a different schema.以下过程设置集合的初始架构版本,然后插入具有不同架构的新文档。
Add a schemaVersion field to the collection将schemaVersion字段添加到集合中
To differentiate between different schemas in the collection, add a 要区分集合中的不同模式,请添加schemaVersion field.schemaVersion字段。
The following command adds a 以下命令将值为schemaVersion field with the value 1 to the documents with the initial schema:1的schemaVersion字段添加到具有初始架构的文档中:
db.contacts.updateMany(
{ },
{
$set: { schemaVersion: 1 }
}
)Insert a document with an updated schema插入具有更新架构的文档
In the updated schema, customers can link their social media accounts. Insert a document that accounts for the new requirements and has a 在更新的模式中,客户可以链接他们的社交媒体帐户。插入一份说明新要求的文档,其schemaVersion of 2:schemaVersion为2:
db.contacts.insertOne(
{
_id: 2,
schemaVersion: 2,
name: "Cameron",
contactInfo: {
cell: "903-555-1122",
work: "670-555-7878",
instagram: "@camcam9090",
linkedIn: "CameronSmith123"
}
}
)Next Steps后续步骤
After you implement the Schema Versioning Pattern, you need to modify how your application queries and updates data.实现架构版本控制模式后,您需要修改应用程序查询和更新数据的方式。
Query the Collection查询集合
Now that there are two different schemas in the 现在contacts collection, your query must check all possible locations for a field value depending on the document's schema version.contacts集合中有两个不同的模式,查询必须根据文档的模式版本检查字段值的所有可能位置。
The following query searches based on the customer's 以下查询基于客户的work number. The query checks both possible locations for the work field:work编号进行搜索。该查询检查work字段的两个可能位置:
db.contacts.find(
{
$or: [
{
work: "503-555-0110"
},
{
"contactInfo.work": "503-555-0110"
}
]
}
)
Output:输出:
{
_id: 1,
name: 'Taylor',
home: '209-555-7788',
work: '503-555-0110',
schemaVersion: 1
}Update a Document in the Collection更新集合中的文档
Similar to inserting data, when you update a collection, your application must check all possible locations for the field to be updated. When you update data, you can use the 与插入数据类似,当您更新集合时,应用程序必须检查要更新的字段的所有可能位置。更新数据时,可以使用schemaVersion field to determine the field to update.schemaVersion字段来确定要更新的字段。
To update the 要更新work phone number for the user with _id: 2, run this command:_id: 2的用户的work电话号码,请运行以下命令:
db.contacts.updateOne(
{ _id: 2 },
[
{
$set: {
"work": {
$cond: {
if: { $eq: [ "$schemaVersion", 1 ] },
then: "999-999-9999",
else: null
}
},
"contactInfo.work": {
$cond: {
if: { $eq: [ "$schemaVersion", 2 ] },
then: "999-999-9999",
else: null
}
}
}
}
]
)
In the previous example:在前面的示例中:
If the matched document's如果匹配文档的schemaVersionis1, then theworkfield is set to the updated value.schemaVersion为1,则work字段设置为更新后的值。If the matched document's如果匹配文档的schemaVersionis2, then thecontactInfo.workfield is set to the updated value.schemaVersion为2,则contactInfo.work字段设置为更新值。