Intro
A simple & interactive guide to understanding CRUD Storage/Database operations created with NodeJS, Mongoose and Mongodb.
Languages:
- HTML
- CSS
- JavaScript
- SQL
Setup
- Before accessing mongoDB datasets, install the required packages:
npm install mongoose
const mongoose = require('mongoose')
- To use an environment Variable(.env), install the required packages:
npm install --save dotenv
require('dotenv').config();
- To Build Applications & APIs, install the required packages:
npm install express
const express = require('express');
- Feel free to install nodemon, which monitors your activity and restarts scripts automatically upon reading changes in code.
npm install --save-dev nodemon
h
Terminal:
Instead of Running:
npm run start
run:
nodemon myApp.js
Create
First, creating a dataset requires input (user-input or auto-generated) as well as developed Schema for the data to fit into.
This process can be synonymous to students attending a new classroom for the first time & being assigned to a seat by the teacher.
You are the teacher and the data are your students.
Creating a schema can be developed in different ways and is going to be dependent on the application as far as data-types, amount of data entered, and the associations chosen between data.
For more information on starting your own mongoose schema, check the docs:Mongoose Schemas
User1 is created after entering:
1.Name
2.Email
3.Todo
**View the Code snippet for example codes*
Anonymous0 user is created after entering:
1.____
2.Email
3.Todo
Feat: Anonymous usernames increment automatically by filtering through the existing array of names that match /Anonymous/ & obtaining the array's length.
Store the modified array in a variable called count
Upon creating a new user, if a name is not stored, then "Anonymous" + count will take undefined's place.
const newUser = new User({
// create new user & email
name:name?name:'Anonymous'+count,
email
})
const sU = await newUser.save() //save user
console.log(sU)
**View the Code snippet for example codes*
testUser is created after entering:
1.Name
2.Email
3._____
Feat: Users who do not store a todo will automatically be replaced with "n/a" .
const todoObj = new Todo({
email: sU.email,
todo:todo ? todo : 'N/A'.toLowerCase()
})
const sTodo = await todoObj.save();
console.log(sTodo)
**View the Code snippet for example codes*
No User is created after reaching the max capacity (10)
// if user is not found in db, create new user
if(!findUser){
if(allUsers.length > 9){
res.sendFile(__dirname + '/views/max.html')
}
**View the Code snippet for example codes*
Retrieve
Retrieve or Read operations in C.R.U.D involves searching data & receiving data.
-Upon visiting one of the three filtered search endpoints, users can filter through data based on that type (users,todos,email).
-Else, users can scroll through the table to view all data.
In the event that the database is empty,
users will not be able to:
- Drop User
- Drop All Users
- Filter todos
- Filter Users
- Filter email
- Change Email
- Change Name
Users will be redirected to a web page that states, "No Users Found".
Remember that classroom analogy above?
Think of reading as roll-call where the teacher is reading off of a list of user's names to determine who is absent or present.
You, the teacher, are reading off of a list & filtering the present students from the absent students
Update
Updating allows the user to change already-existing data.
This process is not Creating a data record. Existing data is in the process of being updated.
A POST request is sent to endpoint, "/form/users/email/change". User input is sent to the body object.
Destructing Assignment is used to extract values from the body's object (user input).
Take advantage of the id property and use it as a filter.
Next, use the model.findById()
function to find the record.
Conditions:
- If current & new email inputs contain an @ and is true
- If a user is found
- If the discovered user's email
findOne.email === currentemail
(input)
If the above conditions are met, execute the model.findOneAndUpdate()
function
example:
User.findOneAndUpdate({email:currentemail},{email:newemail},{new:true})
Update Name with current Name & ID
Update Email with current email & ID
Delete
Deleting specific data takes place with the deleteOne()
function.
await User.deleteOne({_id:id})//user found
Deleting Multiple data takes place with the deleteMany()
function.
const d = await User.deleteMany({})
const e = await Todo.deleteMany({})
Web Link: Database Playground
IG: Instagram - index_daddy0
Git: Github
Currently the web service is set for limited use.
If you want access to the playground, send me a DM and I will restart the service for you.
Alternatively, users can clone the repository
https://github.com/KylesTech95/database-playground-fork.git
and fork for improved use.
Don't forget to add me.
Top comments (0)