本文介绍了Mongo模型可以自我参考吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个这样的模型:
var userSchema = new mongoose.Schema({
_id: { type: Schema.ObjectId },
email: { type: String, unique: true },
ipAddress: { type: String },
referals: [{
type: mongoose.Schema.Types.ObjectId, ref: 'User'
}],
redeem_token: {type: String, unique: true}
});
var User = mongoose.model('User', userSchema);
这可以工作吗?该用户需要参考其他用户.这是为了跟踪注册推荐人.然后,我想使用.Populate,并在引荐中扩展用户[]
Can this work? The user, needs to have a reference to other users. It's to track signup referrals. I want to then use .Populate and expand the users in the referals[]
推荐答案
我正在使用猫鼬.这对我有用,我只是使用this
作为对模型的引用.我有一个Comment
模型.评论中的回复也可以为Comment
.
I'm using Mongoose. This works for me, I'm simply using this
as a reference to the model.I have a Comment
model. Comments can have a reply that is also Comment
.
var Comment = new mongoose.Schema({
id: { type: ObjectId, required: true },
comment: { type: String },
replies: [ this ],
});
这篇关于Mongo模型可以自我参考吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!