0

サッカー クラブに関するデータを含む MongoDB データベースがあります。クラブには選手とシーズンがあります。選手はシーズンごとに異なるチーム番号を持つことができます。

次の Mongoose スキーマを検討してください。

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
      }
    ]
  ]
}

私はこのようなものが欲しい:

{
  players: [
    {
      _id: ObjectId("abc123"),
      person: {
        firstName: "John",
        lastName: "Doe"
      }
      picture: "john.jpg"
    }
  ],
  seasons: [
    name: "2015-2016",
    seasonPlayers: [
      {
        player: {
          person: {
            firstName: "John",
            lastName: "Doe"
          }
          picture: "john.jpg"
        },
        squadNumber: 10
      }
    ]
  ]
}
4

1 に答える 1