我有一个看起来像这样的模型
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。我觉得这样做的方式可能是错误的,这就是为什么它令人困惑或缺少某些东西的原因。
我的问题是在保存用户集合之前如何保存这些集合,以便可以将这些引用包含在我的用户集合中。
与此问题类似,但有多个引用。
使用猫鼬钩子来解决依赖关系。如果要在保存用户之前获取位置附件,请使用pre
挂钩,否则请使用post
挂钩。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句