Mongo Da Best
The M
DataBases
(SQL and NoSQL)
MongoDB Introduction
Mongo in CMD
Mongoose
MongoDB Compass
Schemas & Models
# 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
# FULLSTACK JS
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
- Row or Record
- Columns
- Data is related
- Document
- Field
- Not relational
# FULLSTACK JS
# FULLSTACK JS
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
# 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
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
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 });
# FULLSTACK JS
(The best part)
# FULLSTACK JS
Schemas define the structure of the data or document with Properties and property values.
User Schema
Book Schema
# FULLSTACK JS
The models allow us to communicate with the database collections
Blog Schema
Delete, Update..
MODEL
# FULLSTACK JS
const mongoose =require( 'mongoose');
const { Schema } = mongoose;
const userSchema = new Schema({
first_name: String,
last_name: String,
age: Number
});
# FULLSTACK JS
const userModel=mongoose.model("User",userSchema);
# FULLSTACK JS
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;
}
}
See you in the next session