Mongo 模型可以自我参考吗 [英] Can a Mongo model self reference
本文介绍了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
并在 referals[]
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[]
推荐答案
我正在使用 Mongoose.这对我有用,我只是使用 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 模型可以自我参考吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文