Lab Workbook: Setting Up a Simple Data Model using Mongoose Node API and CRUD Operations on a Mongo Database
This workbook is designed to guide students through the process of setting up a simple data model using the Mongoose Node API.
It includes detailed activities that will help you create, read, update, and delete (CRUD) operations on a Mongo database using Mongoose API calls.
Table of Contents
Introduction to Mongoose and MongoDB
Setting Up the Environment
Creating a Simple Data Model
Implementing CRUD Operations
Conclusion
1. Introduction to Mongoose and MongoDB
Mongoose is a MongoDB object modeling tool designed to work in an asynchronous environment. It provides a straightforward, schema-based solution to model your application data. MongoDB, on the other hand, is a source-available cross-platform document-oriented database program.
Post.deleteOne({ title: 'New Example Title' }, (err) => {
if(err) returnconsole.error(err);
console.log('Deleted successfully');
});
5. Conclusion
Congratulations! You have now set up a simple data model using Mongoose and performed CRUD operations on a MongoDB database. Keep practicing and exploring more functionalities that Mongoose and MongoDB offer.
Remember, the key to mastering any programming language or tool is consistent practice and curiosity. Happy coding!
Lab 2
Learning Outcome:
Write a detailed data model with MONGOOSE NODE API and do create read update delete CRud on a MONGO database and USING MONGOOSE API calls.
https://www.npmjs.com/package/mongoose. Make up an Interesting vertical to provide the example in the context of.
Build a rich MONGO data model such as may be suitable for an Ecommerce Web Application which you will do for your Project.
Mongoose and MongoDB CRUD Operations Workbook
Introduction
Welcome to this student lab learning workbook. In this guide, we will learn how to set up a simple data model using the Mongoose Node.js API. Our practical example will center around an eCommerce web application.
Here's what we'll cover:
Installation and Setup
Building a Data Model
CRUD Operations (Create, Read, Update, Delete)
Prerequisites
Before you start, you should be familiar with:
JavaScript (ES6 syntax)
Node.js
Basic understanding of databases
Let's begin!
Part 1: Installation and Setup
1.1 MongoDB Installation
Firstly, you need to install MongoDB on your machine. Visit MongoDB's official
and follow the instructions for your specific operating system.
1.2 Project Setup
To start with, create a new folder on your computer and navigate into it. This folder will be our project directory. Open your terminal/command prompt and navigate into your newly created folder. Then, initialize a new Node.js project:
bashCopy code
npm init -y
1.3 Mongoose Installation
Once the project is initialized, we need to install Mongoose. Still in the terminal, type:
bashCopy code
npm install mongoose
1.4 Connecting to MongoDB
In your project folder, create a new file called server.js. Inside this file, write the following code to establish a connection to your local MongoDB server:
With this, we have established a connection to a MongoDB database named 'ecommerce'.
Part 2: Building a Data Model
Mongoose works with data models, which are schemas representing the structure of the underlying data in MongoDB. Let's create a simple Product model for our eCommerce application.
Create a new file called productModel.js and add the following: