不遵循创建模型的结构并保存具有不同信息的数据是否有问题?
Is there a problem with not following the structure of the created model and saving data with different information?
我是新手,我正在使用 nodeJS + Express + mongoDB.
做后端
所以我有这个模型:
const user = new Schema({
email: String,
password: String,
lastName: String,
firstName: String
})
module.exports = model('User', user);
然后当用户注册时我保存数据:
const createUser = new User({
email: req.body.email,
password: bcrypt.hashSync(req.body.password, 8),
id: req.body.id,
lastName: req.body.lastName,
firstName: req.body.firstName,
photoUrl: req.body.photoUrl,
});
createUser.save((err, user) => {
if (err) {
res.status(500).send({message: err});
}else{
res.send({message: 'Complete'});
}
}
所以我不知道当我添加主模型中不存在的新数据“photoUrl”时,它是否会影响应用程序或其他 CRUD 功能
Mongoose 默认情况下在模式上有 strict: true
标志,这基本上意味着传递给模型构造函数但未在模式中指定的值不会保存到数据库中。所以基本上所有传递的额外字段都会被跳过。
您必须明确禁用 strict
才能添加数据库中未指定的字段。
以下示例取自 mongoose documentation
// set to false..
const thingSchema = new Schema({..}, { strict: false });
const thing = new Thing({ iAmNotInTheSchema: true });
thing.save(); // iAmNotInTheSchema is now saved to the db!
我是新手,我正在使用 nodeJS + Express + mongoDB.
做后端所以我有这个模型:
const user = new Schema({
email: String,
password: String,
lastName: String,
firstName: String
})
module.exports = model('User', user);
然后当用户注册时我保存数据:
const createUser = new User({
email: req.body.email,
password: bcrypt.hashSync(req.body.password, 8),
id: req.body.id,
lastName: req.body.lastName,
firstName: req.body.firstName,
photoUrl: req.body.photoUrl,
});
createUser.save((err, user) => {
if (err) {
res.status(500).send({message: err});
}else{
res.send({message: 'Complete'});
}
}
所以我不知道当我添加主模型中不存在的新数据“photoUrl”时,它是否会影响应用程序或其他 CRUD 功能
Mongoose 默认情况下在模式上有 strict: true
标志,这基本上意味着传递给模型构造函数但未在模式中指定的值不会保存到数据库中。所以基本上所有传递的额外字段都会被跳过。
您必须明确禁用 strict
才能添加数据库中未指定的字段。
以下示例取自 mongoose documentation
// set to false..
const thingSchema = new Schema({..}, { strict: false });
const thing = new Thing({ iAmNotInTheSchema: true });
thing.save(); // iAmNotInTheSchema is now saved to the db!