我需要保存此文档,但是mongoose Schema包含对象中不存在的字段。如果客户端使用旧API,我想支持这两种变体。
{
"user": "someUser",
"password": "qwerty",
}
const userSchema = new Schema({
user: {
type: String,
required: true
},
password: {
type: String,
required: true
},
preferences: {
banned: {
type: Boolean,
default: false
}
}
})
User.findOneAndUpdate({
user: req.body.user
}, {
password: req.body.password
preferences: {
banned: true
}
}, {
upsert: true,
new: true,
setDefaultsOnInsert: true
}