Express MongodDB Saving Nested Objects – Penang Web Application Development

EXPRESS MONGODB SAVING NESTED OBJECTS

I have more than 10 years of experience in web development and currently residing in Penang island. If you are looking for a web development project, please give me a call. I will be able to assist you. Occasionally I do provide training if you are residing in Penang over a cup of coffee.

How do you save nested objects in MongoDB using Express and Mongoose? This is something every web programmer and developer wants to learn. The first question what is nested data collection in the MongoDB documents? Many college students and coding enthusiasts asked me about MongoDB. They asked me how do I insert data as nested objects.

To understand this process, there will be 3 parts to how we can do this

  1. Creating nested objects schema
  2. Validating nested objects in the schema
  3. Saving Data into the MongoDB

STEP BY STEP APPROACH

Index.js (Main file)

const express = require(‘express’);
const mongoose = require(‘mongoose’);
const router = require(‘./routes/myrouter’);
const app = express();

app.use(express.json());
app.use(express.urlencoded({ extended: true }));

const port = 3000;

// connect to mongodb
mongoose.set(‘strictQuery’, false);
const mongoDB = ‘mongodb+srv://Justin:PASSWORD@rentagf.jtxr2iz.mongodb.net/COLLECTION?retryWrites=true&w=majority’;

// eslint-disable-next-line no-use-before-define
main().catch((err) => console.log(err));
async function main() {
try {
await mongoose.connect(mongoDB);
console.log(‘Database connection established’);
} catch (e) {
console.log(‘Error connecting to database’);
}
}

app.use(‘/register’, router);

app.listen(port, () => console.log(`Example app listening on port ${port}!`));

Routes

const express = require(‘express’);
const app = express();
const path = require(‘path’);
const mycontroller = require(‘../controller/mycontroller’);

const router = express.Router();
app.use(express.static(‘public’));

router.get(‘/register’, (req, res) => {
res.sendFile(path.join(__dirname, ‘../public/index.html’));
});

router.post(‘/register’, mycontroller.myform);

module.exports = router;

Controller

const Inspections = require(‘../model/form’);

exports.myform = (req, res) => {
const data = new Inspections({
certificate_number: req.body.certificate,
business_name: req.body.businessname,
date: req.body.date,
result: req.body.result,
sector: req.body.sector,
address: {
city: req.body.city,
zip: req.body.zip,
street: req.body.street,
number: req.body.number,
},
});

data.save((error) => {
if (error) {
console.log(error);
}
});
res.end();
};

Model

const Inspections = require(‘../model/form’);

exports.myform = (req, res) => {
const data = new Inspections({
certificate_number: req.body.certificate,
business_name: req.body.businessname,
date: req.body.date,
result: req.body.result,
sector: req.body.sector,
address: {
city: req.body.city,
zip: req.body.zip,
street: req.body.street,
number: req.body.number,
},
});

console.log(data);

data.save((error) => {
if (error) {
console.log(error);
}
});
res.end();
};

Public

Public

<form action=”http://localhost:3000/register” method=”post”>

<div class=”p-2″>

<label class=’block’ for=”certificate”>Certificate Number</label>

<input type=”number” name=”certificate”>

</div>

 

<div class=”p-2″>

<label class=’block’ for=”businessname”>Business Name</label>

<input type=”text” name=”businessname”>

</div>

 

<div class=”p-2″>

<label class=’block’ for=”date”>Date</label>

<input type=”date” name=”date”>

</div>

 

<div class=”p-2″>

<label class=’block’ for=”result”>Result</label>

<input type=”text” name=”result”>

</div>

 

<div class=”p-2″>

<label class=’block’ for=”sector”>Sector</label>

<input type=”text” name=”sector”>

</div>

 

<div class=”p-2″>

<label class=’block’ for=”city”>City</label>

<input type=”text” name=”city”>

</div>

 

<div class=”p-2″>

<label class=’block’ for=”zip”>Zip</label>

<input type=”text” name=”zip”>

</div>

 

<div class=”p-2″>

<label class=’block’ for=”street”>Street</label>

<input type=”text” name=”street”>

</div>

 

<div class=”p-2″>

<label class=’block’ for=”number”>number</label>

<input type=”number” name=”number”>

</div>

<br>

<input type=”submit” class=”btn btn-success” name=”submit”>

</form>

</div>

</div>

For more information about Web Development and Web Design Services. Please give us a call. We design many web applications and systems. For more information, please reach us whatsapp.

WEB AND APPS DEVELOPMENT PENANG