Share
Explore

Node.js Fullstack Application Template

Below is a simple Lab Workbook that can guide students in setting up MongoDB with Mongoose and creating 10 sample data records for a men's wear eCommerce website.

testconnectiontomongo.js
megaphone

const mongoose = require('mongoose');


// Connecting to MongoDB
const connectionString = 'mongodb://127.0.0.1:27017/mensWear3DB';
mongoose.connect(connectionString, {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(() => console.log('Connected to MongoDB'))
.catch(err => console.error('Connection error', err));

// Product Schema
const productSchema = new mongoose.Schema({
name: { type: String, required: true },
price: { type: Number, required: true, min: 0 },
category: { type: String, required: true },
inStock: { type: Boolean, default: true }
});

// Creating and Exporting the Model
const Product = mongoose.model('Product', productSchema);
module.exports = Product;

done: LAB WORKBOOK 1: Setting Up MongoDB with Mongoose
=============================================

Introduction
------------
In this lab, we will set up MongoDB with Mongoose and create 10 sample data records for a men's wear eCommerce website. We will cover the installation process, database connection, schema creation, and data insertion.

Step 1: Installing MongoDB
--------------------------
Please follow the official guide to install MongoDB on your system:
https://docs.mongodb.com/manual/installation/

Step 2: Installing Mongoose
---------------------------
Create a new directory for your project and navigate into it. Then run the following command to initialize a Node.js project and install Mongoose:


$ npm init
$ npm install mongoose
Step 3: Connecting to MongoDB using Mongoose and creating a Schema
--------------------------------------------
Create a file called `model.js` This is the mediator between your Application and MongoDB:

const mongoose = require('mongoose');

// Connecting to MongoDB
const connectionString = 'mongodb://127.0.0.1:27017/mensWearDB';
mongoose.connect(connectionString, {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(() => console.log('Connected to MongoDB'))
.catch(err => console.error('Connection error', err));

// Product Schema
const productSchema = new mongoose.Schema({
name: { type: String, required: true },
price: { type: Number, required: true, min: 0 },
category: { type: String, required: true },
inStock: { type: Boolean, default: true }
});
//Creating the MODEL
const Product = mongoose.model('Product', productSchema);

// Exporting the Model
module.exports = Product;
p 4: Creating a Product Schema

Step 5: Generate Starter Data:

Add the following code to create and insert 10 sample product records into the database:

// generateStarterData.js
const mongoose = require('mongoose');
const Product = require('./model'); // Importing the Product model

mongoose.connect('mongodb://127.0.0.1:27017/mensWearDB', {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(() => {
console.log('Connected to MongoDB');
// Use the Product model here
const product = new Product({
name: 'Shirt',
price: 29.99,
category: 'Tops',
inStock: true
});

product.save().then(() => {
console.log('Product saved');
mongoose.connection.close();
});
})
.catch(err => console.error('Connection error', err));


Working code to insert data:
megaphone

const mongoose = require('mongoose');


// Connecting to MongoDB
const connectionString = 'mongodb://127.0.0.1:27017/mensWear20DB';
mongoose.connect(connectionString, {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(() => console.log('Connected to MongoDB'))
.catch(err => console.error('Connection error', err));

// Product Schema
const productSchema = new mongoose.Schema({
name: { type: String, required: true },
price: { type: Number, required: true, min: 0 },
category: { type: String, required: true },
inStock: { type: Boolean, default: true }
});

// Creating the MODEL
const Product = mongoose.model('Product', productSchema);

// Use the Product model here
const product = new Product({
name: 'Shirt',
price: 29.99,
category: 'Tops',
inStock: true
});

product.save().then(() => {
console.log('Product saved');
mongoose.connection.close();
});

// Exporting the Model
module.exports = Product;

Now, the code first connects to the MongoDB instance, then it defines a schema and model for a product, creates an instance of a product, saves it, and exports the product model.

Step 6: Running the Code

Run the following command to execute the code:

$ node app.js

You should see a message indicating a successful connection to MongoDB and confirmation that the data has been added.
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.