[英]how to save data to mongodb schema with object
I've been trying to save user data to a mongodb schema, sample schema below, the question is how can i insert data inside the object.我一直在尝试将用户数据保存到 mongodb 模式,示例模式如下,问题是如何在 object 中插入数据。 Please help i've been doing this for hours
请帮助我已经这样做了几个小时
eg User.js例如 User.js
const mongoose = require('mongoose');
const UserSchema = new mongoose.Schema({
data:{
type: String,
requred: true
},
data2:{
type: String,
requred: true
},
work: {
industry: String,
name: String,
position: String,
},
status:{
type: Boolean,
default: true,
requred: true
},
subscription:{
product: String,
license: String,
price: String,
status: Boolean,
default: false,
renew: Boolean,
default : false
},
date:{
type: Date,
default: Date.now
}
});
const User = mongoose.model('User', UserSchema);
module.exports = User;
sample registration handling样品注册处理
const newUser = new User({
data1,
data2,
work,
subscription
});
newUser.save()
.then(user => {
req.flash('success_mg', 'Successfully Registered your can now Login.');
res.redirect('/login');
})
.catch(err => console.log(err));
You want to use the _id field, and call your schema like this:您想使用 _id 字段,并像这样调用您的架构:
User.findByIdAndUpdate({_id: id}, {...updates}, {new: true})
.then(updated_user=>{ DO SOMETHING... })
the ...updates would be a JSON object containing the keys and values of the properties you're updating. ...更新将是 JSON object 包含您正在更新的属性的键和值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.