我有这样的模型:
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);
Run Code Online (Sandbox Code Playgroud)
这可以吗?用户需要具有对其他用户的引用.这是跟踪注册推荐.我想然后使用.Populate并扩展referals []中的用户
rat*_*rat 23
我正在使用猫鼬.这对我有用,我只是this用作模型的参考.我有一个Comment模特.评论也可以回复Comment.
var Comment = new mongoose.Schema({
id: { type: ObjectId, required: true },
comment: { type: String },
replies: [ this ],
});
Run Code Online (Sandbox Code Playgroud)
小智 5
I know this question is old. But I stumbled upon this as I was looking for a solution to a similar problem. So, this is for future knowledge seekers!
Ankur's answer will help if you want the referals created inside the user document. Ex:
{
_id: 'XXX',
...
referals: [{_id:'yyy',email: ''}]
}
Run Code Online (Sandbox Code Playgroud)
我认为使用Mongoose Virtuals将有助于更好地扩展应用程序。使用虚拟记录,您不必创建重复的记录。
因此,如果您决定使用 Mongoose Virtuals,您的架构将如下所示
{
_id: 'XXX',
...
referals: [{_id:'yyy',email: ''}]
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助。如果我错了,请纠正我,因为我是新手。
| 归档时间: |
|
| 查看次数: |
7434 次 |
| 最近记录: |