我有一个MongoDB数据库,其中包含有关足球俱乐部的数据。俱乐部有球员和赛季。每个赛季球员都可以拥有不同的球队号码。
考虑以下Mongoose模式:
InjectionConstructor
如何检索完全填充的const clubSchema = new mongoose.Schema({
players: [playerSchema],
seasons: [seasonSchema]
});
const personSchema = new mongoose.Schema({
firstName: String,
lastName: String
});
const playerSchema = new mongoose.Schema({
person: personSchema,
picture: String
});
const seasonSchema = new mongoose.Schema({
name: String,
seasonPlayers: [seasonPlayerSchema]
});
const seasonPlayerSchema = new mongoose.Schema({
player: {
type: mongoose.Schema.Types.ObjectId,
ref: "Player"
},
squadNumber: Number
});
文档?所以不要得到这样的东西:
Club
我想要这样的事情:
{
players: [
{
_id: ObjectId("abc123"),
person: {
firstName: "John",
lastName: "Doe"
}
picture: "john.jpg"
}
],
seasons: [
name: "2015-2016",
seasonPlayers: [
{
player: ObjectId("abc123"),
squadNumber: 10
}
]
]
}
答案 0 :(得分:0)
您可以在Mongoose的population路径中使用点表示法来指定要填充的子文档层次结构中的路径:
Club.findOne().populate('seasons.seasonPlayers.player').exec(function(err, club) {...});