[英]how to save data to mongodb schema with object
我一直在嘗試將用戶數據保存到 mongodb 模式,示例模式如下,問題是如何在 object 中插入數據。 請幫助我已經這樣做了幾個小時
例如 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;
樣品注冊處理
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));
您想使用 _id 字段,並像這樣調用您的架構:
User.findByIdAndUpdate({_id: id}, {...updates}, {new: true})
.then(updated_user=>{ DO SOMETHING... })
...更新將是 JSON object 包含您正在更新的屬性的鍵和值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.