Javascript 如何在mongoDB和模式中保存数据如下
下面是我使用mongoose npm包的模式Javascript 如何在mongoDB和模式中保存数据如下,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,Mongoose,下面是我使用mongoose npm包的模式 var StatusSchema = new mongoose.Schema({ empName: { projectName: { type: String }, clientName: { type: String }, statusLastWeek: { type: String }, statusThisweek: { type: String }, p
var StatusSchema = new mongoose.Schema({
empName: {
projectName: { type: String },
clientName: { type: String },
statusLastWeek: { type: String },
statusThisweek: { type: String },
planNextWeek: { type: String }
}
});
下面是我的nodejs代码来更新数据
var Status = mongoose.model('Status', StatusSchema);
module.exports = Status;
Description: Want save data in MongoDB, data schema is like above mentioned,
save saving data is sored loke as bellow.
Inside Mongo DB :
{ "_id" : ObjectId("5d92f4aba4695e2dd90ab438"), "__v" : 0 }
{ "_id" : ObjectId("5d92f4b4a4695e2dd90ab439"), "__v" : 0 }
MongoDB中的预期集合:
Dave Smith {
projectName: BLE Mesh,
clientName: Tera,
statusLastWeek: BLE Scan,
statusThisweek: BLE List View,
planNextWeek: Mqtt config
}
在这里您可以看到我的NodeJS代码:
router.post ('/update', (req,res,next)=>{
userStatus = new wkStatus(req.body)
userStatus.save()
.then(status => {
res.redirect('/success');
console.log ("Status saved in DB")
})
.catch(err => console.log(err))
// return next;
});
试试这种方法,希望它能奏效。如果需要更多信息,您可以向我发送消息您试图创建的模式本身是错误的
empName: {
projectName: { type: String },
clientName: { type: String },
statusLastWeek: { type: String },
statusThisweek: { type: String },
planNextWeek: { type: String }
}
上面的模式可以创建如下对象:“empName”不能是动态的
empName: {
projectName: BLE Mesh,
clientName: Tera,
statusLastWeek: BLE Scan,
statusThisweek: BLE List View,
planNextWeek: Mqtt config
}
如果您想像所显示的那样在empName
处动态存储,则应将empName
设置为Map
请参见错误是什么?没有这样的错误,数据以下面的格式保存{“\u id”:ObjectId(“5d92f4aba4695e2dd90ab438”),“\u v”:0}{“\u id”:ObjectId(“5d92f4b4a4695e2dd90ab439”),“\u v”:0}如果保存正确,问题是什么?没有saylok相同的问题,数据的保存方式与之前的“\u id”:ObjectId”相同(“5d9304bd632777375de2bdec”),“__v”:0}>>我的前端:员工姓名项目名称客户姓名上周状态下周计划本周状态功能要保存员工每周状态的n个数字,员工姓名/EMP ID将是唯一的,但项目名称和客户名称可以是公用的。以前MongoDB模式类似于empName:{type:String,unique:true,required:true,},projectName:{type:String,unique:true,required:true,},clientName:{type:String,},statusLastWeek:{type:String,},statusThisweek:{type:String,},planNextWeek:{type:String},那时我遇到了错误@{”错误:{“driver”:true,“name”:“MongoError”,“index”:0,“code”:11000,“errmsg”:“E11000重复键错误集合:wp.status index:projectName\u 1 dup key:{:\“BLE Mesh\”}}}}请删除存储区中的所有数据,然后重试,希望它能工作var courseSchema=new mongoose.Schema({empName:{type:String,required:'此字段是必需的!'},projectName:{type:String},clientName:{type:String},sLatWeek:{type:String},sThisWeek:{type:String},pNextWeek:{type:String},});mongoose.model('Course',courseSchema);这是我的mongo模式,我想插入n个雇员记录。
empName: {
projectName: BLE Mesh,
clientName: Tera,
statusLastWeek: BLE Scan,
statusThisweek: BLE List View,
planNextWeek: Mqtt config
}
//You can use ODM like mongoose and define a schema with mongoose.Schema. You can just
// see mongoose module document from npm. Use .save() for save an object in DB.
// Example :
// schema as admin
const mongoose = require('mongoose');
mongoose.Promise = global.Promise;
const Schema = mongoose.Schema;
const bcrypt = require('bcrypt-nodejs');
const sha256 = require('sha256')
const adminSchema = new Schema({
fullName: { type: String, required: true },
userName: { type: String },
noc: { type: String, required: true },
mobileNumber: { type: String, required: true },
email: { type: String },
chacommAddress: {
contactPerson: { type: String },
country: { type: String },
address: { type: String },
city: { type: String },
pinCode: { type: String },
state: { type: String },
stateCode: { type: String },
},
address: {
country: { type: String },
city: { type: String },
pinCode: { type: String },
state: { type: String },
stateCode: { type: String },
address: { type: String },
CIN: { type: String },
GSTIN: { type: String }
},
password: { type: String, required: true },
userType: { type: Number, required: true },
createdAt: { type: Date, required: true },
uploadFile: { type: String, required: true },
bankdetails: {
bankName: { type: String },
accountNo: { type: String },
ifscCode: { type: String },
accountType: { type: String },
accountName: { type: String },
cancelledChequeCopy: { type: String }
},
isActive: { type: Boolean },
invoiceString:{type:String},
invoiceValue:{type:Number},
accountantName :{type:String} ,
accountantDesignation : {type:String},
referredBy:{type:String}
});
adminSchema.methods.comparePassword = function (password) {
let password_hash = sha256(password);
return bcrypt.compareSync(password_hash, this.password);
}
adminSchema.pre('save', function (next) {
if (!this.isModified('password'))
return next();
let password_hash = sha256(this.password);
bcrypt.hash(password_hash, null, null, (err, hash) => {
if (err)
return next(err);
this.password = hash;
next();
});
});
//export schema
// module.exports = mongoose.model('Admin', adminSchema)
// for save:
const admin = require('admin')
var obj= new admin({
// values as per model defined
})
obj.save()