MongoDB过滤嵌套对象

时间:2019-03-31 19:50:41

标签: node.js database mongodb mongoose

我正在研究一个足球投注项目,在该项目中,球队的比赛名称为history。每一场比赛都是比赛的一部分。这意味着比赛的各个步骤都有比赛

我有以下型号

const MatchSchema = new mongoose.Schema({
  local: { type: mongoose.Schema.Types.ObjectId, ref: 'Team' },
  guest: { type: mongoose.Schema.Types.ObjectId, ref: 'Team' },
  localScore: { type: Number, default: -1 },
  guestScore: { type: Number, default: -1 },
  date: { type: mongoose.Schema.Types.Date },
});

const StepSchema = new mongoose.Schema({
  matchs: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Match', default: [] }],
  name: String,
});

const CompetitionSchema = new mongoose.Schema({
  steps: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Step', default: [] }],
  start: { type: mongoose.Schema.Types.Date },
  name: String,
});

const TeamSchema = new mongoose.Schema({
  name: String,
  logo: String,
  history: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Match', default: [] }],
});

我正在排名排行榜上,我想选择参加特定比赛的球队。我想通过过滤来获取所有在特定比赛中比赛的球队。

我检查了汇总方法,但我不知道如何告诉mongo他必须填充字段才能使投影起作用,以及如何将这些匹配项与比赛ID相关联。

我希望我说清楚了

0 个答案:

没有答案