1

我有一个看起来像这样的模型

var UserSchema = new Schema({
    id:                 ObjectId,
    username:           { type: String, trim: true, required: true, unique: true, lowercase: true },
    location:           [{ type: ObjectId, ref: 'Location', required: false }],
    attachments:        [{ type: ObjectId, ref: 'Attachment', required: false }]
})

当用户注册时,我需要在保存/创建用户以获取这些 ObjectId 之前创建位置和附件对象。我觉得我这样做的方式可能是错误的,这就是为什么它令人困惑或者我缺少一些东西。

我的问题是如何在保存用户集合之前保存这些集合,以便我可以将这些引用包含在我的用户集合中。

类似于这个问题,除了多个参考。

4

1 回答 1

0

使用猫鼬钩子解决依赖关系。如果您想在保存用户之前获取位置附件,请使用pre钩子,否则使用post钩子。

于 2014-11-18T09:25:26.013 回答