查询后是否可以更新文档(插入新字段?)>
我正在尝试根据用户电子邮件进行查询(mongodb + node.js),此后,我想插入一个新字段。到目前为止,我已经尝试使用user.save(),但是从我看到的结果来看,这只能更新找到的文档中现有字段的值。请帮帮我。
const UserSchema = new mongoose.Schema({
firstname: {
type: String,
required: true,
trim: true,
email: {
type: String,
required: true,
trim: true,
lowercase: true,
validate(value) {
if (!validator.isEmail(value)) {
throw new Error('Email is invalid')
}
}
}
}})
const User = mongoose.model('User', UserSchema);
User.findOne({ email: email }).then(user => {
user.xxx=some_variable
user.save()
})
我正在尝试根据用户电子邮件进行查询(mongodb + node.js),此后,我想插入一个新字段。到目前为止,我已经尝试使用user.save(),但是从我的观察中只能看到...
回答如下:是的,但是我认为newField必须是UserSchema中定义的字段之一。