MongoDB / Mongoose 有很多关系
MongoDB / Mongoose has many Relationship
所以我想弄清楚 Mongo/Mongoose 中的人口和关系。我有一个会有很多预测的 Fixture。如果我想显示一个预测的夹具,我可以简单地使用 populate 方法,但是如果我需要显示一个夹具的所有预测怎么办?
这听起来很简单,但也许我 SQL 仍在努力弄清楚。这很容易做到还是我以错误的方式接近它?这是我的模型 -
var FixtureSchema = new Schema({
homeTeam: {
type: Number,
required: true,
ref: 'Team'
},
awayTeam: {
type: Number,
required: true,
ref: 'Team'
},
date: {
type: Date
},
matchday: {
type: Number,
required: true
}
});
var PredictionSchema = new Schema({
goalsHomeTeam: {
type: Number,
required: true
},
goalsAwayTeam: {
type: Number,
required: true
},
fixture: {
type: Number,
ref: 'Fixture'
},
user: {
type: Schema.ObjectId,
ref: 'User'
}
});
您必须先找到您想要的灯具,然后搜索您的预测并找到匹配项。 Mongo 没有 'reverse' 人口,但编写自己的人口很容易:
Prediction.find({fixture: fixture._id}, function(err, predictions) {
//do things with predictions
})
所以我想弄清楚 Mongo/Mongoose 中的人口和关系。我有一个会有很多预测的 Fixture。如果我想显示一个预测的夹具,我可以简单地使用 populate 方法,但是如果我需要显示一个夹具的所有预测怎么办?
这听起来很简单,但也许我 SQL 仍在努力弄清楚。这很容易做到还是我以错误的方式接近它?这是我的模型 -
var FixtureSchema = new Schema({
homeTeam: {
type: Number,
required: true,
ref: 'Team'
},
awayTeam: {
type: Number,
required: true,
ref: 'Team'
},
date: {
type: Date
},
matchday: {
type: Number,
required: true
}
});
var PredictionSchema = new Schema({
goalsHomeTeam: {
type: Number,
required: true
},
goalsAwayTeam: {
type: Number,
required: true
},
fixture: {
type: Number,
ref: 'Fixture'
},
user: {
type: Schema.ObjectId,
ref: 'User'
}
});
您必须先找到您想要的灯具,然后搜索您的预测并找到匹配项。 Mongo 没有 'reverse' 人口,但编写自己的人口很容易:
Prediction.find({fixture: fixture._id}, function(err, predictions) {
//do things with predictions
})