我是mongoose和mongoDB的新手,通过教程,我的用户架构如下所示:
var userSchema = mongoose.Schema({
local: {
email: {
type: String,
index: {
unique: true,
dropDups: true
}
},
password: String,
displayName: String,
avatar: {
type: String,
default: "./img/user.png"
},
role: {
type: String,
default: "student"
},
ask_history: [
{
question_id: {
type: mongoose.Schema.Types.ObjectId,
ref: 'questionAnswer'
},
favorite: Boolean,
ask_time: Date
}
],
interest: [String]
},
facebook: {
id: String,
token: String,
email: String,
displayName: String,
avatar: String,
familyName: String,
givenName: String,
gender: String,
ageMin: Number,
role: {
type: String,
default: "student"
},
ask_history: [
{
question_id: {
type: mongoose.Schema.Types.ObjectId,
ref: 'questionAnswer'
},
favorite: Boolean,
ask_time: Date
}
],
interest: [String]
},
twitter: {
id: String,
token: String,
email: String,
name: String,
avatar: String,
role: {
type: String,
default: "student"
},
ask_history: [
{
question_id: {
type: mongoose.Schema.Types.ObjectId,
ref: 'questionAnswer'
},
favorite: Boolean,
ask_time: Date
}
],
interest: [String]
}
}, {strict: true});
它通过用户注册的方式创建用户记录,或者发送本地电子邮件或者使用第三方的oauth注册。
如果我创建本地用户,上面的模式将生成如下文档:
{
"_id": {
"$oid": "58792ee6e8a08204a0d68f7a"
},
"twitter": { ////////////////////////////////////////
"interest": [], //
"ask_history": [], //
"role": "student" //
}, // <-- nothing should created for a local user
"facebook": { //
"interest": [], //
"ask_history": [], //
"role": "student" //
}, ////////////////////////////////////////
"local": {
"password": "$2a$10$OtdbF7t52TyNAuwZXFT0u.Q/A.E5TeV4T.shHCxSSxDll2nX4bCbW",
"displayName": "aaa",
"email": "aaa@gmail.com",
"interest": [],
"ask_history": [],
"role": "admin",
"avatar": "./img/user.png"
},
"__v": 0
}
我遇到的问题是
ask_history: [
{
question_id: {
type: mongoose.Schema.Types.ObjectId,
ref: 'questionAnswer'
},
favorite: Boolean,
ask_time: Date
}
],
interest: [String]
在模式中没有那些数组声明,文档看起来对任何类型的用户都很好,例如,本地用户,文档看起来像这样(没有facebook或twitter字段):
var userSchema = mongoose.Schema({
local: {
email: {
type: String,
index: {
unique: true,
dropDups: true
}
},
password: String,
displayName: String,
avatar: {
type: String,
default: "./img/user.png"
},
role: {
type: String,
default: "student"
}
},
facebook: {
id: String,
token: String,
email: String,
displayName: String,
avatar: String,
familyName: String,
givenName: String,
gender: String,
ageMin: Number,
role: {
type: String,
default: "student"
}
},
twitter: {
id: String,
token: String,
email: String,
name: String,
avatar: String,
role: {
type: String,
default: "student"
}
}
}, {strict: true});
{
"_id": {
"$oid": "5874d1d0cd045371b4e96eca"
},
"local": {
"password": "$2a$10$KpnIE2Uc2tX.YfcQCGm6EeDMexFFZXbKJVcQvxltBxrTkxb8E7mH.",
"displayName": "aaa",
"avatar": "./img/user.png",
"email": "aaa@gmail.com",
"role": "admin"
},
"__v": 0
}
这是我创建本地用户的功能:
passport.use('local-signup', new LocalStrategy({
usernameField: 'email',
passwordField: 'password',
passReqToCallback: true
}, function(req, email, password, done) {
if (!req.body.name || req.body.name.length == 0) {
return done(null, false, req.flash('signupMessage', 'Name can\'t be empty'));
}
User.findOne({
'local.email': email
}, function(err, user) {
if (err) {
return done(null, false, req.flash('signupMessage', 'Error: ' + err));
} else {
if (validator.validate(email)) {
if (user) {
return done(null, false, req.flash('signupMessage', 'Email already registered.'));
} else {
var newUser = new User();
newUser.local.email = email;
newUser.hashPassword(password);
newUser.local.displayName = req.body.name;
newUser.save(function(err) {
if (err) {
throw err;
}
return done(null, newUser);
});
}
} else {
return done(null, false, req.flash('signupMessage', 'Email not vaild'));
}
}
});
}));
我的问题是如何设置架构到创建文档时,不会创建数组。