Javascript 多个帐户的嵌套关联
我正在尝试将各种登录方法与Javascript 多个帐户的嵌套关联,javascript,mongodb,mongoose,model-associations,Javascript,Mongodb,Mongoose,Model Associations,我正在尝试将各种登录方法与mongo中相同的用户模型相关联 const UserSchema = new Schema({ email: String isVerified: { default: false, type; Boolean } accounts: { ref: 'Account', type: Schema.Types.ObjectId } }) const AccountSchema = new Schema({ facebook: {
mongo
中相同的用户
模型相关联
const UserSchema = new Schema({
email: String
isVerified: { default: false, type; Boolean }
accounts: {
ref: 'Account',
type: Schema.Types.ObjectId
}
})
const AccountSchema = new Schema({
facebook: {
ref: 'FacebookAccount',
type: Schema.Types.?
},
local: {
ref: 'LocalAccount',
type: Schema.Types.?
},
twitter: {
ref: 'TwitterAccount',
type: Schema.Types.?
},
})
const LocalAccount = new Schema({
email: String,
name: String,
phone: String,
password: String,
_user: {
ref: 'User',
type: Schema.Types.ObjectId
}
})
我希望得到的数据是:
{
_id: '12345789',
email: 'turdFerguson@gmail.com',
accounts: {
facebook: { ... }
local: { ... }
}
}
我真的不确定这些关联,尽管因此
Schema.Types.?
在个人帐户上。也不确定我是否应该使用嵌入式vs对象引用,以及在哪里合适。我在兜圈子,试图让关联匹配起来。我建议您使用embedded保持简单
下面是一个快速建议:
const UserSchema = new Schema({
isVerified: {
default: false,
type: Boolean
},
accounts: {
local: {
email: String,
name: String,
phone: String,
password: String
},
facebook: {
// fields related to Facebook
},
twitter: {
// fields related to Twitter
}
}
})
我删除了
电子邮件
,因为它似乎是多余的,因为您已经拥有帐户.local.email
为什么不为此使用一个集合?“这似乎太过分了。”米奇,实际上我从来没有想过。我想这是一个可行的想法。搜索是否存在用户,然后根据用于身份验证的登录方法将数据存储在相应的帐户中。我将对此进行调查,看看它是否与我正在努力实现的目标相符。谢谢你的主意。谢谢你的建议,这样做容易多了,而且工作起来很有魅力!