{ JavaScript } MongoDB
MongoDB
What is MongoDB?
- MongoDB is an open-source database program that organizes data by using documents.
- Uses JSON-like documents with optional schemas.
- Uses key-value pairing to associate data objects with properties.
- MongoDB Atlas is the global cloud database on AWS, Azure, and GCP
- MongoDB also has an ORM that is called Mongoose.
NoSQL Databases (SQL VS NOSQL)
- SQL - Structured data schema(Strict) & data is set to several tables linked with a ‘relationship’
- NoSQL - No Schema, No relationship, and JSON-like data format
Commands
-
use {dbname}: Create db (if it doesn’t exist) & switch to a specific db → Note that you won’t see your new database in the list of databases until data is added.
-
Specify a collection name = data model: storing all documents related to that model within the same group.
e.g. within recipe app(db) , there is a group (collection) named as ‘contacts’, and its data (documents)
-
Insert:
- db.inventory.insertOne
- db.inventory.insertMany
-
Find:
- find({key:”value”}) - findOne - findMany db.contacts.find({id: ObjectId(“6241cb3b6d1c4b70d50a75b1”)}) db.contacts.find({name:”Jon”})
-
Delete
- db.collection.deleteMany()
- db.collection.deleteOne()
-
Update
- What is the use of ‘set’ in MongoDB?
- In MongoDB, the $set operator is used to replace the value of a field to the specified value.
- What is the use of ‘set’ in MongoDB?
Syntax: , { $set: { <field1>: <value1>, ... } }
db.contacts.update({name:"jon wexler"}, {$set:{email:”jonjwexler@gmail.com”}})
- select a db: use choose_db
- show all dbs: show dbs
- show collections in a db: use choose_db then show collections
- show all records in a db: use choose_db then db.users.find()
- delete a db: use choose_db then db.dropDatabase()
- to see which db is being used: db
Setting up Mongoose with your Node.js application
Index.js (main.js)
const express = require("express");
const mongoose = require("mongoose");
const app = express();
const port = 3000;
/**
* Connect to our Mongo DB
* We are returned a promise; then & catch
* tells what to do when it succeeds .(then) and
* what to do when it fails (.catch)
*/
mongoose
.connect("mongodb://localhost:27017/usersDB2", {
useNewUrlParser: true,
})
.then((data) => {
console.log("Mongo DB connection success!");
})
.catch((err) => {
console.log("Mongo DB connection failed: " + err.message);
});
// Create a schema
const userSchema = new mongoose.Schema({
name: String,
email: String,
fav_pizza: String,
personalities: [String],
});
// Apply it to a model
const User = mongoose.model("User", userSchema);
// User has to be with capital first letter. mongo will make that small and pluralize
// the model name: "User" => "users"
// Creating
const pj = new User({
name: "PJ",
email: "BIGID@gmail.com",
fav_pizza: "Pepperoni",
personalities: ["Challenging","Kind"],
});
const hj = new User({
name: "Hyukjoo Lee",
email: "lee@gmail.com",
fav_pizza: "Pepperoni",
personalities: ["Dedicated", "Detailed"],
});
User.insertMany([pj, hj, cr], function (err) {
if (err) {
console.log(err);
} else {
console.log("Successfully created db");
}
});
// Create a simple path for the home page
// Will take the usual request, response,
// and next arguments needed for this sort of function
app.use("/", (req, res, next) => {
res.json({
confirmation: "success",
data: "This is the mongo project!",
});
});
app.listen(() => {
console.log("app is listening " + port);
});
Creating a schema
-
As mentioned, MongoDB itself is a schema-less database. However, Mongoose enforces schemas on our behalf to help maintain data integrity.
-
Note that MongoDB has ORM (Object Relational Mapping)
Read.js
const mongoose = require("mongoose");
//create and/or connect to a db
mongoose.connect("mongodb://localhost:27017/usersDB2", {
useNewUrlParser: true,
});
// Create a schema
const userSchema = new mongoose.Schema({
name: String,
email: String,
fav_pizza: String,
personalities: [String],
});
// Apply it to a model
const User = mongoose.model("User", userSchema);
// User has to be with capital first letter. mongo will make that small and pluralize
// the model name: "User" => "users"
// Read All Data
User.find(function (err, users) {
if (err) {
console.log(err);
} else {
// console.log(users);
// if we want to show only the names the following code does
users.forEach(function (user) {
console.log(user);
// closing the connection: good practice
mongoose.connection.close();
});
}
});
Update.js
User.updateOne(
{ _id: "624210c0601f212f1c76367e" },
{ name: "updated" },
function (err) {
if (err) {
console.log(err);
} else {
console.log("marking");
}
}
);
Delete.js
User.deleteOne({ _id: "624210c0601f212f1c76367e" }, function (err) {
if (err) {
console.log(err);
} else {
console.log("deleted");
}
});