{MongoDB}
Mongo Da Best
The M
1
DataBases
(SQL and NoSQL)
2
MongoDB Introduction
3
Mongo in CMD
5
Mongoose
4
MongoDB Compass
6
Schemas & Models
DataBases
# FULLSTACK JS
SQL: structured query language.
NoSQL: Not only structured query language.

SQL
NoSQL
# FULLSTACK JS
//NoSQL user1: FirstName, LastName user2: LastName, Age //SQL FirstName, LastName, Age user1: Maria, Oussadi, NULL user2: NULL, Casablancas, 44
The Differences
How is the data stored?
# FULLSTACK JS
There

It stores the data in tables, and you can have as many tables as you like
It stores the data in anything but a table, can be documents, graphs..etc
The differences between:
- Row or Record
- Columns
- Data is related
SQL
NoSQL
- Document
- Field
- Not relational
# FULLSTACK JS
SQL & NoSQL
# FULLSTACK JS
Introduction
An open-source database available on all operating systems, launched in 2009 the same year as nodeJS, which is coincidental but the NodeJS community definitely fell in love with MongoDb and it became the choice for many NodeJS developers.
It stores data in JSON files and they're called collections, the db can have as many as you like

Terminologies
A Database = A number of collections
A collection = A number of documents
A document = A number of fields
Create your first Mongo database
https://account.mongodb.com/account/login
MongoDB in CMD
Let's get on the Shell
# FULLSTACK JS
mongosh //Start the MongoDB server mongo //Open MongoDBs Shell show dbs //Show the list of DBs use dbname //Use or Create new DB show collections //Show collections db.help //Get methods db.collectionName.insertOne({ //Insert into or create //parameters }) db.collectionName.find({ //Find data //parameters })
CMD Commands
# FULLSTACK JS
db.collectionName.updateOne({ // Update data (one update) //search parameters }, {"$set: {.. parameters to update}"} db.User.updateMany({query},{$set: {parameters}}) //Many Updates db.User.deleteOne({firstame: "abd"}) // Delete one document, or use deleteMany to delete many data db.User.drop() //drop all data exit //to exit the shell
CMD Commands
MongoDB Compass
IT HELPS IN:
Explore collections and documents inside GUI.
Signing on the MongoDB website
Explore existing clusters
Create a new cluster
Connect to DB using URI
# FULLSTACK JS
Mongoose
What is it?
Is an Object Document Mapping (ODM) library for MongoDB and Node.js. It manages relationships between data, and helps us to connect and communicate with the database
# FULLSTACK JS
Install Mongoose
npm i mongoose // install it from npm
const mongoose = require('mongoose') // we require it
# FULLSTACK JS
Connecting to the Database
mongoose.connect('mongodb://localhost:27017/DBName'); //put the link of the db
# FULLSTACK JS
Connecting to the Database
const URI= 'mongodb://localhost:27017/Demo' mongoose.connect(URI) .then((connection) => { console.log("db connected"); }) .catch((reason) => { console.error(reason); process.exit(-1); });
Add the NewUrlParser
mongoose.connect(URI,{ useNewUrlParser: true });
Schemas and Models
# FULLSTACK JS
(The best part)
Schemas
# FULLSTACK JS
Schemas define the structure of the data or document with Properties and property values.
User Schema
Book Schema
- Name: String, required
- age: Number
- Bio: String, required
- Title: String, required
- Author: String, required
- Year: String, required
# FULLSTACK JS
Models
The models allow us to communicate with the database collections
Blog Schema
Delete, Update..
MODEL
Creating Schema
# FULLSTACK JS
const mongoose =require( 'mongoose'); const { Schema } = mongoose; const userSchema = new Schema({ first_name: String, last_name: String, age: Number });
# FULLSTACK JS
Creating a Model
const userModel=mongoose.model("User",userSchema);
# FULLSTACK JS
Creating Data
async function createData(first_name,last_name,age){ try{ const user = await userModel.create({first_name,last_name,age}); return user; }catch(e){ return null; } }
Thank you for attending
See you in the next session
MongoDB Training
By Maria Oussadi
MongoDB Training
- 124