Use the inheritance pattern when your documents are mostly similar and you want to keep them in the same collection so they can be read together. The inheritance pattern uses a parent entity with common fields to group child entities that have variable forms. The child entities can have unique fields, but are closely related to one another due to their common fields.当文档大多相似,并且您希望将它们保存在同一集合中,以便可以一起阅读时,请使用继承模式。继承模式使用具有公共字段的父实体来对具有可变形式的子实体进行分组。子实体可以具有唯一的字段,但由于它们的公共字段而彼此密切相关。
About this Task关于此任务
In this example, a book store uses the inheritance pattern to store different types of media. 在这个例子中,书店使用继承模式来存储不同类型的媒体。A book parent entity stores common fields like title and author, and multiple child entities inherit from the book entity. book父实体存储title和author等公共字段,多个子实体从book实体继承。For example audio books, printed books, and ebooks have common fields, and also have unique fields specific to the media type.例如,有声读物、印刷书籍和电子书有共同的字段,也有特定于媒体类型的独特字段。
The inheritance pattern stores these slightly different entities in the same collection, which improves performance for queries that need to access all books, regardless of type.继承模式将这些略有不同的实体存储在同一个集合中,这提高了需要访问所有书籍(无论类型如何)的查询的性能。
Steps步骤
Insert the sample data.插入样本数据。
db.books.insertMany( [
{
product_type: "ebook",
title: "Practical MongoDB Aggregations",
author: "Paul Done",
rating: 4.8,
genres: [ "programming" ],
pages: 338,
download_url: "<url>"
},
{
product_type: "audiobook",
title: "Practical MongoDB Aggregations",
author: "Paul Done",
rating: 4.6,
genres: [ "programming" ],
narrators: [ "Paul Done" ],
duration: {
hours: 21,
minutes: 8
},
time_by_chapter: [
{
chapter: 1,
start: "00:00:00",
end: "01:00:00"
},
{
chapter: 2,
start: "01:00:00",
end: "01:55:00"
}
]
},
{
product_type: "physical_book",
title: "Practical MongoDB Aggregations",
author: "Paul Done",
rating: 4.9,
genres: [ "programming" ],
pages: 338,
stock: 12,
delivery_time: 2
}
] )
The preceding documents share some common fields, and have unique fields depending on the 前面的文档共享一些公共字段,并且根据product_type. For example:product_type具有唯一的字段。例如:
ebookdocuments have a文档有一个download_urlfield.download_url字段。audiobookdocuments have a文档有一个time_by_chapterfield.time_by_chapter字段。physical_bookdocuments have a文档有一个delivery_timefield.delivery_time字段。
Query all documents.查询所有文档。
Even though the documents in the 即使books collection have different shapes, you can return all documents with a single query:books集合中的文档具有不同的形状,您也可以通过一个查询返回所有文档:
db.books.find()
Output:输出:
[
{
_id: ObjectId('66eb4160ef006be6eda8e2ee'),
product_type: 'ebook',
title: 'Practical MongoDB Aggregations',
author: 'Paul Done',
rating: 4.8,
genres: [ 'programming' ],
pages: 338,
download_url: '<url>'
},
{
_id: ObjectId('66eb4160ef006be6eda8e2ef'),
product_type: 'audiobook',
title: 'Practical MongoDB Aggregations',
author: 'Paul Done',
rating: 4.6,
genres: [ 'programming' ],
narrators: [ 'Paul Done' ],
duration: { hours: 21, minutes: 8 },
time_by_chapter: [
{ chapter: 1, start: '00:00:00', end: '01:00:00' },
{ chapter: 2, start: '01:00:00', end: '01:55:00' }
]
},
{
_id: ObjectId('66eb4160ef006be6eda8e2f0'),
product_type: 'physical_book',
title: 'Practical MongoDB Aggregations',
author: 'Paul Done',
rating: 4.9,
genres: [ 'programming' ],
pages: 338,
stock: 132,
delivery_time: 2
}
]Query unique fields.查询唯一字段。
The inheritance pattern does not require additional logic to query on fields that are specific to a particular media type. For example, the following query returns books that have a duration longer than 20 hours, which only applies to the product type 继承模式不需要额外的逻辑来查询特定于特定媒体类型的字段。例如,以下查询返回持续时间超过20小时的书籍,这仅适用于产品类型audio_book:audio_book:
db.books.find(
{
"duration.hours": { $gt: 20 }
}
)
Output:输出:
[
{
_id: ObjectId('66eb4160ef006be6eda8e2ef'),
product_type: 'audiobook',
title: 'Practical MongoDB Aggregations',
author: 'Paul Done',
rating: 4.6,
genres: [ 'programming' ],
narrators: [ 'Paul Done' ],
duration: { hours: 21, minutes: 8 },
time_by_chapter: [
{ chapter: 1, start: '00:00:00', end: '01:00:00' },
{ chapter: 2, start: '01:00:00', end: '01:55:00' }
]
}
]