mongoose add data to collection code example
Example 1: node mongoose save document
// Example for a model called Person with two properties, e.g. name, surname
// Values to be saved
const person = { name: Jane, surname: Doe};
// you can access the _id property from savedPerson variable (savedPerson._id)
savedPerson = await new Person(person).save()
Example 2: mongoose add document
const Product = require('../models/product.js');
const product = new Product();
product.save()
.then(doc => {})
.catch(err => {});
Example 3: mongoose add data to collection
const Name =require('path of your model')
app.use('/path', (req,res,next)=>
{
const firstName = req.body.firstName // req.body.firstName refers to your form name input
const lastName = req.body.lastName // req.body.lastName refers to your form name input
const nameData = new Name({firstName:firstName, lastName:lastName});
nameData.save().then(res.redirect('/')).catch(err){
console.log(err);
};
});
//Remember to add a model
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const addittionSchema = new Schema({
firstName:{
type: String,
required: True
},
lastName:{
type: String,
required:True
}
});
module.exports = mongoose.model('budget', addittionSchema);