Database Manual / Data Modeling / Schema Design Patterns / Polymorphic Data

Store Polymorphic Data存储多态数据

Store polymorphic data when you need to access documents that have different fields or data types together in the same query.当您需要在同一查询中同时访问具有不同字段或数据类型的文档时,请存储多态数据。

MongoDB uses a flexible data model, which means documents in a single collection do not need to have the same structure. Polymorphic data is data in a single collection that varies in document fields or data types.MongoDB使用灵活的数据模型,这意味着单个集合中的文档不需要具有相同的结构。多态数据是指单个集合中的数据,其文档字段或数据类型各不相同。

About this Task关于此任务

In this example, your application stores professional athletes who play different sports. Your queries access all athletes, but the attributes stored for each athlete vary depending on their sport.在这个例子中,应用程序存储了从事不同运动的专业运动员。查询可以访问所有运动员,但为每个运动员存储的属性因运动而异。

The polymorphic pattern stores different document shapes in the same collection, which improves performance for queries that need to access all athletes regardless of sport.多态模式将不同的文档形状存储在同一个集合中,这提高了需要访问所有运动员的查询的性能,无论他们从事何种运动。

Steps步骤

1

Insert the sample data.插入样本数据。

db.athletes.insertMany( [
{
sport: "bowling",
name: "Earl Anthony",
career_earnings: 1440000,
perfect_games: 25,
pba_championships: 43,
events: [
{
name: "japan_pba",
score: 300,
year: 1972
}
]
},
{
sport: "tennis",
name: "Steffi Graf",
career_earnings: 21000000,
grand_slam_wins: 22,
surfaces: [ "grass", "clay", "hard court" ]
},
{
sport: "cricket",
name: "Sachin Tendulkar",
career_earnings: 8000000,
runs: 15921,
centuries: 51,
teammates: [ "Arshad Ayub", "Kapil Dev" ]
}
] )
2

Query all documents.查询所有文档。

Even though the documents in the athletes collection have different fields, you can return all documents with a single query:即使athletes集合中的文档有不同的字段,您也可以通过一个查询返回所有文档:

db.athletes.find()

Output:输出:

[
{
_id: ObjectId('6706dcd66fd2c3b24f2e7e92'),
sport: 'bowling',
name: 'Earl Anthony',
career_earnings: 1440000,
perfect_games: 25,
pba_championships: 43,
events: [ { name: 'japan_pba', score: 300, year: 1972 } ]
},
{
_id: ObjectId('6706dcd66fd2c3b24f2e7e93'),
sport: 'tennis',
name: 'Steffi Graf',
career_earnings: 21000000,
grand_slam_wins: 22,
surfaces: [ 'grass', 'clay', 'hard court' ]
},
{
_id: ObjectId('6706dcd66fd2c3b24f2e7e94'),
sport: 'cricket',
name: 'Sachin Tendulkar',
career_earnings: 8000000,
runs: 15921,
centuries: 51,
teammates: [ 'Arshad Ayub', 'Kapil Dev' ]
}
]
3

Query unique fields.查询唯一字段。

The polymorphic pattern does not require additional logic to query on fields that are specific to a particular sport. For example, the following query returns athletes that have more than 20 grand slam wins, which only applies to athletes who play tennis:多态模式不需要额外的逻辑来查询特定运动的特定字段。例如,以下查询返回赢得20场大满贯以上的运动员,这仅适用于打网球的运动员:

db.athletes.find(
{ grand_slam_wins: { $gt: 20 } }
)

Output:输出:

[
{
_id: ObjectId('6706cd8a6fd2c3b24f2e7e8d'),
sport: 'tennis',
name: 'Steffi Graf',
career_earnings: 21000000,
grand_slam_wins: 22,
surfaces: [ 'grass', 'clay', 'hard court' ]
}
]

Learn More了解更多