猫鼬-如何获取子文档值以插入到另一个文档中?

时间:2019-02-05 15:37:34

标签: mongoose subdocument

插入新的锦标赛时,“模式”和“规则”类似这样:

"mode" : ObjectId("5c4cdc8ad439c01d2061f335"),
"rule" : ObjectId("5c4cdc8ad439c01d2061f336")

我不仅要获取ObjectId,还要获取两者的名称和描述等数据。

我有这两个模型:

game.model.js

const rulesSchema = new Schema({
    name: String,
    description: String
});

const platformSchema = new Schema({
    name: String
});

const modesSchema = new Schema({
    name: String,
    description: String
});

const gameSchema = new Schema({
    name: String,
    platforms: [platformSchema],
    modes: [modesSchema],
    rules: [rulesSchema],
    isValidate: Boolean,
    author: String,
    date: Date
});

tournament.model.js

const ObjectId = mongoose.Schema.Types.ObjectId;

const tournamentSchema = new Schema({
    game: ObjectId,
    name: String,
    description: String,
    mode: ObjectId,
    rule: ObjectId,
    online: Boolean,
    nbPlayers: Number,
    startDate: Date,
    place: String,
    dateCreation: Date,
    author: String     
});

我尝试了几种解决方案,但不确定如何在两个模型之间建立“关系”。

最佳做法是什么?我是否必须存储整个子文档(我在视图中使用Ionic)还是仅需要ID?

谢谢!

0 个答案:

没有答案