-
Every req in MongoDBBoilerplate (Full Example) List of Express JS:
//api naming convention
1. app.get('bookings')
2. app.get('bookings/:id')
3. app.post('/bookings')
4. app.patch('/bookings/:id')
4. app.delete('/bookings/:id')
*/
All Opareation Client > Database > Mongodb
========================================
======== Get Method (Email waise data fetch in MongoDb) =================
<---Client Code--->
1.
https://car-showroom-server.vercel.app/products?email=${user?.email}
database .
app.get("/bookings", async (req, res) => {
const email = req.query.email;
console.log(email)
const query = { buyerEmail: email };
const products = await bookingsCollection.find(query).toArray();
res.send(products);
});
2.
const { user } = useContext(AuthContext);
const [orders, setOrders] = useState([]);
useEffect(() => {
fetch(`http://localhost:5000/orders?email=${user?.email}`)
.then((res) => {
// if (res.status === 401 || res.status === 403) {
// return logout();
// }
return res.json();
})
.then((data) => {
setOrders(data);
});
}, [user?.email]);
<---Database Code--->
//orders api
app.get("/orders", async (req, res) => {
let query = {};
if (req.query.email) {
query = {
email: req.query.email,
};
}
const result = await orderCollection.find(query).toArray();
res.send(result);
});
=========
const url = `http://localhost:5000/bookings?email=${user?.email}`;
const { data: bookings = [] } = useQuery({
queryKey: ["bookings", user?.email],
queryFn: async () => {
const res = await fetch(url, {
headers: {
authorization: `bearer ${localStorage.getItem("accessToken")}`,
},
});
const data = await res.json();
console.log(data);
return data;
},
});
//
app.get("/bookings", async (req, res) => {
const email = req.query.email;
// console.log(email)
// const decodedEmail = req.decoded.email;
// if (email !== decodedEmail) {
// return res.status(403).send({ message: 'forbidden access' });
// }
const query = { email: email };
const bookings = await bookingsCollection.find(query).toArray();
res.send(bookings);
});
========================================
======== Method ( MongoDB ObjectId বাদে অন্য id ধরে data load ) =======
<---Client Code--->
const { serviceName, phone, price, service, _id, customer, status } = order;
const [orderService, setOrderService] = useState([]);
useEffect(() => {
fetch(`http://localhost:5000/services/${service}`)
.then((res) => res.json())
.then((data) => setOrderService(data));
}, [service]);
=================
{
path: "/category/:name",
element: <CategoryProducts />,
loader: ({ params }) =>
fetch(`http://localhost:5000/category/${params.name}`),
},
<Link
to={`/category/${category?.name}`}
key={category._id}
className="category"
>
<div className="text-center">
<h4 className="text-[#010c3a] capitalize text-xl font-semibold mb-3 ">
{category?.name}
</h4>
<div className="icon m-auto">
<img src={category?.picture} alt={category?.name} />
</div>
</div>
</Link>
const category = useLoaderData();
<---Database Code--->
app.get("/category/:name", async (req, res) => {
const name = req.params.name;
const query = { name };
const categoryName = await categoriesCollection.findOne(query);
res.send(categoryName);
});
========================================
<--- Delete Method (_id waise) --->
<---Client Code--->
const handleDelete = (id) => {
const proceed = window.confirm(
"Are you sure you want to cancel this order"
);
if (proceed) {
fetch(`http://localhost:5000/orders/${id}`, {
method: "DELETE",
})
.then((res) => res.json())
.then((data) => {
console.log(data);
if(data.deletedCount > 0){
alert('deleted successfully')
const remaining = orders.filter(odr => odr._id !== id);
setOrders(remaining)
}
});
}
};
//
<button onClick={() => handleDelete(_id)} className="btn btn-circle">
<---Database Code--->
//delete method
app.delete("/orders/:id", async (req, res) => {
const id = req.params.id;
const query = { _id: ObjectId(id) };
const result = await orderCollection.deleteOne(query);
res.send(result);
});
========================================
<--- Update Method ( add admin, pending/ approved, req/submited work in this way ) --->
<---Client Code--->
<button
onClick={() => handleStatusUpdate(_id)}
className="btn btn-ghost btn-xs"
>
{status ? status : "pending"}
</button>
//
const handleStatusUpdate = (id) => {
fetch(`http://localhost:5000/orders/${id}`, {
method: "PATCH",
headers: {
"content-type": "application/json",
},
body: JSON.stringify({ status: "Approved" }),
})
.then((res) => res.json())
.then((data) => {
console.log(data);
if (data.modifiedCount > 0) {
console.log(data);
const remaining = orders.filter((odr) => odr._id !== id);
const approving = orders.find((odr) => odr._id === id);
approving.status = "Approved";
const newOrders = [approving, ...remaining];
setOrders(newOrders);
}
});
};
<---Database Code--->
app.patch("/orders/:id", async (req, res) => {
const id = req.params.id;
const status = req.body.status;
console.log(id, status);
const query = { _id: ObjectId(id) };
const updatedDoc = {
$set: {
status: status,
},
};
const result = await orderCollection.updateOne(query, updatedDoc);
res.send(result);
});
========================================
<--- Get Method (double query করে data load MOdule : 68) --->
<---Client Code--->
const [products, setProducts] = useState([]);
const [size, setSize] = useState(10);
useEffect(() => {
const url = `http://localhost:5000/products?page=${page}&size=${size}`;
fetch(url)
.then((res) => res.json())
.then((data) => {
setProducts(data.products);
setCount(data.count);
});
}, [page, size]);
<---Database Code--->
app.get("/products", async (req, res) => {
const page = parseInt(req.query.page);
const size = parseInt(req.query.size);
console.log(page, size);
const query = {};
const cursor = productCollection.find(query);
const products = await cursor
.skip(page * size)
.limit(size)
.toArray();
//pagination er jonno count send kora client site
const count = await productCollection.estimatedDocumentCount();
res.send({ products, count });
});
========================================
<--- post Method ( map body to db id) --->
<---Client Code--->
//get data from localStorage
useEffect(() => {
const storedCart = getStoredCart();
const savedCart = [];
const ids = Object.keys(storedCart);
console.log(storedCart, ids);
fetch("http://localhost:5000/productsByIds", {
method: "POST",
headers: {
"content-type": "application/json",
},
body: JSON.stringify(ids),
})
.then((res) => res.json())
.then((data) => {
for (const id in storedCart) {
const addedProduct = data.find((product) => product._id === id);
if (addedProduct) {
const quantity = storedCart[id];
addedProduct.quantity = quantity;
savedCart.push(addedProduct);
}
}
setCart(savedCart);
});
}, [products]);
<---Database Code--->
app.post("/productsByIds", async (req, res) => {
const ids = req.body;
const objectIds = ids.map(id => ObjectId(id));
const query = {_id: {$in: objectIds}};
console.log(ids, query) ;
const cursor = productCollection.find(query);
const products = await cursor.toArray();
res.send(products);
});
========================================
<--- get Method ( module 74.5 & 74.6 doctor portal) --->
<---Client Code--->
const [treatment, setTreatment] = useState(null);
const date = format(selectedDate, "PP");
const {
data: appointmentOptions = [],
refetch,
isLoading,
} = useQuery({
queryKey: ["appointmentOptions", date],
queryFn: async () => {
const res = await fetch(
`http://localhost:5000/v2/appointmentOptions?date=${date}`
// `http://localhost:5000/v2/appointmentOptions?date=${date}`
);
const data = await res.json();
return data;
},
});
//
<div className="max-w-[1400px] mx-auto mt-8 grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-8">
{appointmentOptions.map((option) => (
<AppointmentOption
key={option._id}
option={option}
setTreatment={setTreatment}
/>
))}
</div>
<---Database Code--->
//use aggregate to query multiple collection and then merge data
app.get("/appointmentOptions", async (req, res) => {
const date = req.query.date;
const query = {};
const options = await appointMentOptionCollection.find(query).toArray();
console.log(options);
//get the booking of the provided date
const bookingQuery = { appointmentDate: date };
const alreadyBooked = await bookingsCollection
.find(bookingQuery)
.toArray();
//code carefully :D
options.forEach((option) => {
const optionBooked = alreadyBooked.filter(
(book) => book.treatment === option.name
);
const bookedSlots = optionBooked.map((book) => book.slot);
const remainingSlots = option.slots.filter(
(slot) => !bookedSlots.includes(slot)
);
option.slots = remainingSlots;
console.log(option.name, remainingSlots.length);
});
res.send(options);
});
========================================
<--- Method ( name diya finde kore onno collection theke data fetch kore same name er all data show ) --->
<---Client Code--->
{
path: "/category/:name",
element: <CategoryProducts />,
loader: ({ params }) => {
return fetch(`http://localhost:5000/category/${params.name}`);
},
},
const products = useLoaderData();
<---Database Code--->
const productsCollection = client.db("car_showroom").collection("products");
const categoriesCollection = client.db("car_showroom").collection("categories");
// get all categories
app.get("/categories", async (req, res) => {
const query = {};
const result = await categoriesCollection.find(query).toArray();
res.send(result);
});
// get single category item
app.get("/category/:name", async (req, res) => {
const name = req.params.name;
const query = { category: name };
const categoryName = await productsCollection.find(query).toArray();
res.send(categoryName);
});
========================================
<--- Method () --->
<---Client Code--->
<---Database Code--->
//temporary to update price field on all products
app.put("/products", async (req, res) => {
const filter = {};
const options = { upsert: true };
const updatedDoc = {
$set: {
sellerPrice: 90,
},
};
const result = await productsCollection.updateMany(
filter,
updatedDoc,
options
);
res.send(result);
});
========================================
<--- post Method ( MongoDB-তে duplicate data ইনসার্ট প্রিভেন্ট করতে কে কোন মেথড ইউজ করেন?) --->
<---Client Code--->
<---Database Code--->
// MongoDB-তে duplicate data ইনসার্ট প্রিভেন্ট করতে কে কোন মেথড ইউজ করেন?
// Data insert korar aage data ache kina sheta condition diye check kore erpor insert kori. For example amar ekta api share korlam
app.post("/users", async (req, res) => {
const { username, email, institute, address, photo } = req.body;
const user = await usersCollection.findOne({
$or: [{ email }, { username }],
});
if (user) {
res.status(400).send({
message: "Username or email already exists",
});
} else {
const newUser = {
username,
email,
institute,
address,
photo,
};
await usersCollection.insertOne(newUser);
res.status(200).send({
message: "User created successfully",
});
}
});
========================================
<--- Get Method (sort by price low to high ) --->
// module 70_5-3
<---Client Code--->
const [services, setServices] = useState([]);
const [isAsc, setIsAsc] = useState(true);
useEffect(() => {
fetch(`http://localhost:5000/services?order=${isAsc ? "asc" : "desc"}`)
.then((res) => res.json())
.then((data) => setServices(data));
}, [isAsc]);
<button
onClick={() => setIsAsc(!isAsc)}
>
{isAsc ? "desc" : "ase"}
</button>
<---Database Code--->
// Example : 1
//all service fetch sort by price low to high ,
app.get("/services", async (req, res) => {
const query = {};
const order = (req.query.order === "asc" ? 1 : -1);
console.log(order)
const result = await serviceCollection
.find(query)
.sort({ price: order })
.toArray();
res.send(result);
});
// Example : 2
//all service fetch sort by price low to high ,
app.get("/services", async (req, res) => {
// down to 100 price services fetch
// const query = { price: { $lt: 100 } };
// up to 100 price services fetch
// const query = { price: { $gt: 100 } };
// up to 20 and down to 300 price services fetch
const query = { price: { $gt: 20, $lt: 180 } };
const order = req.query.order === "asc" ? 1 : -1;
console.log(order);
const result = await serviceCollection
.find(query)
.sort({ price: order })
.toArray();
res.send(result);
});
========================================
<--- Get Method (search filed to find produt ) --->
<---Client Code--->
const searchRef = useRef();
const [isAsc, setIsAsc] = useState(true);
const [search, setSearch] = useState("");
useEffect(() => {
fetch(
`http://localhost:5000/services?search=${search}&order=${
isAsc ? "asc" : "desc"
}`
)
.then((res) => res.json())
.then((data) => setServices(data));
}, [isAsc, search]);
const handleSearch = () => {
setSearch(searchRef.current.value);
};
console.log(search);
// search
<div className="search-from flex items-center justify-center gap-4 mx-auto pb-4">
<input
ref={searchRef}
type="text"
placeholder="Search Service"
className="input input-bordered input-primary w-full max-w-xs"
/>
<button onClick={handleSearch} className="btn btn-primary">
Search
</button>
</div>
// ase / dese
<button
onClick={() => setIsAsc(!isAsc)}
className="border border-[#FF3811] btn-sm ml-7 rounded-[5px] text-[#FF3811] font-semibold hover:bg-[#FF3811] hover:text-white hover:duration-1650"
>
{isAsc ? "desc" : "ase"}
</button>
<---Database Code--->
//services api
//all service fetch sort by price low to high ,
app.get("/services", async (req, res) => {
const search = req.query.search;
console.log(search);
let query = {};
if (search.length) {
query = {
$text: {
$search: search,
},
};
}
// const query = { price: { $gt: 10, $lt: 380 } };
const order = req.query.order === "asc" ? 1 : -1;
const result = await serviceCollection
.find(query)
.sort({ price: order })
.toArray();
res.send(result);
});
========================================
১। mongodb atlas data host korte dai pore data niya kaj korte pari
2.mongodb connect korar jonno (URI) thakbe
=>const uri = ;
3. mongodb oi URI er jonno ekta Client dei jaite oi URI (update, post, get, delete) korte pari and URI pass korte hobe
=> const client = new MongoClient(uri);
4. Sei client ke Connect kora and Database er kaj async kaj
async function run() {
await client.connect();
}
5. app.use(express.json())
6.
0. npm install mongodb
1. create New Project (copy user and password)
2. Database > Connect > connect your application copy (include full drive) paste index.js
//
const { MongoClient, ServerApiVersion } = require('mongodb');
const uri = "mongodb+srv://GeniusCar:<password>@cluster0.nftlnia.mongodb.net/?retryWrites=true&w=majority";
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true, serverApi: ServerApiVersion.v1 });
client.connect(err => {
const collection = client.db("test").collection("devices");
// perform actions on the collection object
client.close();
});
3. Remove this and create async function
client.connect(err => {
const collection = client.db("test").collection("devices");
// perform actions on the collection object
client.close();
});
4. create async function (follow mongodb crud find many)
async function run(){
try{
//4.1 create collection
const serviceCollection = client.db("geniusCar").collection(services);
}
finally{
}
}
run().catch(error => console.error(error))
5. Database > collections > create database same name
client.db("geniusCar").collection("services");
database name (geniusCar)
collection(services)
6. Backend data load করতে api lagbe
app.get('/services', async(req, res) => {
})
7. query কারে কারে find করতে চাই ( )
const query = { runtime: { $lt: 15 } };
৮। সব গুলাকে find করতে চাইলে empty object দিতে হবে।
const query = { };
৯। data sort করতে options use করে।
const options = {
// sort returned documents in ascending order by title (A->Z)
sort: { title: 1 },
// Include only the `title` and `imdb` fields in each returned document
projection: { _id: 0, title: 1, imdb: 1 },
};
10. অথবা find use করতে cursor নিবো।
const query = { };
const cursor = serviceCollection.find(query);
const express = require("express");
const cors = require("cors");
const { MongoClient, ServerApiVersion } = require("mongodb");
require("dotenv").config();
const app = express();
const PORT = process.env.PORT || 5000;
//middleware
app.use(cors());
app.use(express.json());
const uri = `mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASSWORD}@cluster0.nftlnia.mongodb.net/?retryWrites=true&w=majority`;
const client = new MongoClient(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
serverApi: ServerApiVersion.v1,
});
async function run() {
try {
const serviceCollection = client.db("geniusCar").collection(services);
//Backend data load করতে api lagbe
app.get("/services", async (req, res) => {
//সব গুলাকে find করতে চাইলে empty object দিতে হবে।
const query = {};
//data sort করতে options use করে। অথবা find use করতে cursor নিবো।
const cursor = serviceCollection.find(query);
// async/ promise call হচ্ছে তাই await use করতে হবে
//toArray দিয়ে cursor টাকে array তে convert করতে হবে যাতে client site use করতে হবে
const services = await cursor.toArray();
res.send(services);
});
} finally {
}
}
run().catch((error) => console.error(error));
app.get("/", (req, res) => {
res.send("Hello Genius Car Server");
});
app.listen(PORT, () => {
console.log("genius car server is running", PORT);
});
============================================
//mongodb
0. npm install mongodb
1. create New Project (copy user and password)
2. Database > Connect > connect your application copy (include full drive) paste index.js
//
const { MongoClient, ServerApiVersion } = require('mongodb');
const uri = "mongodb+srv://GeniusCar:<password>@cluster0.nftlnia.mongodb.net/?retryWrites=true&w=majority";
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true, serverApi: ServerApiVersion.v1 });
client.connect(err => {
const collection = client.db("test").collection("devices");
// perform actions on the collection object
client.close();
});
3. Remove this and create async function
client.connect(err => {
const collection = client.db("test").collection("devices");
// perform actions on the collection object
client.close();
});
4. create async function (follow mongodb crud find many)
async function run(){
try{
//4.1 create collection
const serviceCollection = client.db("geniusCar").collection('services');
}
finally{
}
}
run().catch(error => console.error(error))
5. Database > collections > create database same name
client.db("geniusCar").collection(services);
database name (geniusCar)
collection(services)
6. Backend data load করতে api lagbe
app.get('/services', async(req, res) => {
})
7. query কারে কারে find করতে চাই ( )
const query = { runtime: { $lt: 15 } };
৮। সব গুলাকে find করতে চাইলে empty object দিতে হবে।
const query = { };
৯। data sort করতে options use করে।
const options = {
// sort returned documents in ascending order by title (A->Z)
sort: { title: 1 },
// Include only the `title` and `imdb` fields in each returned document
projection: { _id: 0, title: 1, imdb: 1 },
};
10. অথবা find use করতে cursor নিবো।
const query = { };
const cursor = serviceCollection.find(query);
11. data create করার জন্য Express er post method use করতে হয়
//orders api call
app.post("/orders", async (req, res) => {
const order = req.body;
const result = orderCollection.insertOne(order);
res.send(result);
});
12. // call post method
fetch("http://localhost:5000/orders", {
method: "POST",
headers: {
"content-type": "application/json",
},
body:JSON.stringify(order)
})
.then(res=> res.json)
.then(data=> console.log(data))
.then(error => console.error(error))
13, //api get korte hole
app.get("/orders", async (req, res) => {
const query = {};
const cursor = orderCollection.find(query);
const orders = await cursor.toArray();
res.send(orders);
});
14, delete server site
// delete method
app.delete("/orders/:id", async (req, res) => {
const id = req.params.id;
const query = { _id: ObjectId(id) };
const result = await orderCollection.deleteOne(query);
res.send(result);
});
15. delete client site code
const handleDelete = (id) => {
const proceed = window.confirm("Are you sure, you want ");
if (proceed) {
fetch(`http://localhost:5000/orders/${id}`, {
method: "DELETE",
})
.then((res) => res.json())
.then((data) => {
console.log(data);
if (data.deletedCount > 0) {
alert("deleted successfully");
const remaining = orders.filter(odr=> odr._id !== id);
setOrders(remaining)
}
});
}
};
14. patch server code
//patch
app.patch('/orders/:id', async(req, res) => {
const id = req.params.id;
const status = req.body.status;
const query = {_id:ObjectId(id)};
const updateDoc = {
$set:{
status: status
}
}
const result = await orderCollection.updateOne(query, updateDoc);
res.send(result)
})
14. patch client site code
//patch
const handleStatusUpdate = (id) => {
fetch(`http://localhost:5000/orders/${id}`, {
method: "PATCH",
Headers: {
"content-type": "application/json",
},
body: JSON.stringify({ status: "Approved" }),
})
.then((res) => res.json())
.then((data) => {
console.log(data);
if (data.modifiedCOunt > 0) {
const remaining = orders.filter((odr) => odr._id !== id);
const approving = orders.find(odr => odr._id === id);
approving.status = 'Approved'
const newOrders = [...remaining, approving];
setOrders(newOrders);
}
});
};
MongoDB CRUD operation নিয়ে simple talk ?
C ---- Create বা insert Data =>
Server থেকে নতুন Data কে .insertOne() , .insertMany() দিয়ে MongoDB র Collection এ সেই নতুন Data পাঠিয়ে দেয়া ।
R ----- Read Data =>
MongoDB র Collection থেকে কোনো Data কে ব্যাবহার করার জন্য .find() দিয়ে Data কে Server কিংবা Client Side এ ফিরিয়ে আনা ।
U ----- Update Data =>
MongoDB র Collection এ থাকা কোনো Data কে পরিবর্তন করার জন্য .updateOne() , .updateMany() , replaceOne() ব্যাবহার করে Server Side থেকে MongoDB তে Data পাঠানো ।
D ---- Delete Data =>
MongoDB Collection এ থাকা কোনো Data কে Delete করার জন্য .deleteOne() , .deleteMany() দিয়ে Server থেকে MongoDB তে কমান্ড পাঠানো ।
//Number 1 of CRUD POST method
// POST (Crud er => C )
//Step 1 : (send data client to server)
const handleAddServant = (event) => {
event.preventDefault();
console.log(servant);
//(send data client to server)
fetch('http://localhost:5000/servants', {
method: 'POST',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify(servant)
})
.then(res => res.json())
.then(data => {
if(data.acknowledged){
alert('User added successfully');
event.target.reset();
}
})
};
//Step 2 : (receive data in server)
async function run() {
try {
const servantCollection = client
.db("servant-database")
.collection("servants");
//(receive data in server)
app.post('/servants', async (req, res) => {
const servant = req.body;
console.log(servant);
const result = await servantCollection.insertOne(servant)
res.send(result);
});
} finally {
}
}
run().catch(console.dir);
//Number 2 of CRUD GET/READ method
//Get api (Crud => R(get/read) )
async function run() {
try {
const servantCollection = client
.db("servant-database")
.collection("servants");
//Get api (Crud => R(get/read) )
//you can access browser (http://localhost:5000/servants)
app.get("/servants", async (req, res) => {
const query = {};
const cursor = servantCollection.find(query);
const servants = await cursor.toArray();
res.send(servants);
});
} finally {
}
}
run().catch(console.dir);
//Number 3 of CRUD PUT/PATCH method
//Update (PUT/PATCH) api (Crud => u(PUT/PATCH) )
async function run() {
try {
const servantCollection = client
.db("servant-database")
.collection("servants");
//Update (PUT/PATCH) api (Crud => u(PUT/PATCH) )
app.put("/servants/:id", async (req, res) => {
const id = req.params.id;
const query = { _id: ObjectId(id) };
const servant = req.body;
const option = { upsert: true };
const updatedServant = {
$set: {
name: servant.name,
address: servant.address,
email: servant.email,
},
};
const result = await servantCollection.updateOne(query, updatedServant, option);
res.send(result);
});
}
run().catch(console.dir);
app.listen(Port, () => {
console.log(`Servant Network Server running on port ${Port}`);
});
//Update Component
import React, { useState } from "react";
import { useLoaderData } from "react-router-dom";
const Update = () => {
const storedServant = useLoaderData();
const [servant, setServant] = useState(storedServant);
const handleUpdateServant = (event) => {
event.preventDefault();
console.log(servant);
fetch(`http://localhost:5000/servants/${storedServant._id}`, {
method: "PUT",
headers: {
"content-type": "application/json",
},
body: JSON.stringify(servant),
})
.then((res) => res.json())
.then((data) => {
if (data.modifiedCount > 0) {
alert("user updated");
console.log(data);
event.target.reset()
}
});
};
const handleInputChange = (event) => {
const field = event.target.name;
const value = event.target.value;
const newUser = { ...servant };
newUser[field] = value;
setServant(newUser);
};
return (
<div className="text-center">
<h2>Update Servant: {storedServant.name} </h2>
<h2>Update Servant: {storedServant.email} </h2>
<div className="">
<form onSubmit={handleUpdateServant} className="mx-auto mb-14 max-w-md">
<input
type="text"
name="name"
defaultValue={storedServant.name}
required
onBlur={handleInputChange}
placeholder="Type Name"
className="input input-bordered input-secondary w-full max-w-xs"
/>
<br />
<input
type="text"
name="address"
required
onBlur={handleInputChange}
defaultValue={storedServant.address}
placeholder="Type address"
className="input input-bordered input-secondary w-full max-w-xs"
/>
<br />
<input
type="email"
name="email"
required
defaultValue={storedServant.email}
onBlur={handleInputChange}
placeholder="Type Email"
className="input input-bordered input-secondary w-full max-w-xs"
/>
<br />
<button className="btn btn-outline btn-success" type="submit">
Update Servant
</button>
</form>
</div>
</div>
);
};
export default Update;
//Number 4 of CRUD DELETE method
//DELETE api (Crud => D(DELETE) )
async function run() {
try {
const servantCollection = client
.db("servant-database")
.collection("servants");
//Delete api (CRUD => D )
app.delete("/servants/:id", async (req, res) => {
const id = req.params.id;
const query = { _id: ObjectId(id) };
const result = await servantCollection.deleteOne(query);
console.log(result);
res.send(result);
});
} finally {
}
}
run().catch(console.dir);
//delete component
const Blog = () => {
const servants = useLoaderData();
const [displayServants, setDisplayServants] = useState(servants);
const handleDelete = (servant) => {
const agree = window.confirm(
`Are you sure you want to delete: ${servant.name}`
);
if (agree) {
console.log(agree);
fetch(`http://localhost:5000/servants/${servant._id}`, {
method: "DELETE",
})
.then((res) => res.json())
.then((data) => {
if (data.deletedCount > 0) {
alert("user deleted successfully");
const remainingServant = displayServants.filter(
(srvnt) => srvnt._id !== servant._id
);
setDisplayServants(remainingServant);
}
});
}
};
return (
<div className="text-center">
<h2>Blog</h2>
<h1>servant: {displayServants.length}</h1>
<div>
{displayServants.map((servant) => (
<div className="p-4 mb-3 border" key={servant._id}>
<p>{servant.name}</p>
<p>{servant.email}</p>
<button
className="btn btn-tiny"
onClick={() => handleDelete(servant)}
>
X
</button>
</div>
))}
</div>
</div>
);
};
export default Blog;
/*
১। npm install dotenv --save
2 create .env file in your root folder
DB_USER=Genius
DB_PASSWORD=ylqSoHGMEeM8
3. inde.js file (change username and password)
require('dotenv').config()
const uri = `mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASSWORD}@cluster0.nftlnia.mongodb.net/?retryWrites=true&w=majority`;
*/
//Three places to setore images
1. Thirt party Image hosting server
2. File system of your server
3. mongodb (databasae)
- Notes must be know every single part for interview
************Mongo DB Notes************
//Module 65-8
1.What are MongoDb operators?
MongoDb offers the following query operator types:
i. Comparison
ii. Logical
iii. Element
iv. Evalution
v. Geospatial
vi. Array
vii. Bitwise
viii. Comments
************End Node Notes************
must be know every single part for interview https://roadmap.sh/react
************Mongo DB Interview Questions************
//Milestone: 11 Backend and Database integrate
//Node.js Interview Questions
//Module 65.9
1. What is Node.js and how it works?
2. What are the key features of Node.js?
3. What in npm? What is the main functionality of npm?
4. What is the difference between JavaScript and Node.js?
5. What is event-driven programming in Node.js?
6. How single threaded handles concurrency when multiple I/O operations happing in Node.js?
7. What is package.json?
8. What is Event loop in Node.js and how does it work?
9. What do you understand by callback hell?
//MongoDb Interview Questions
1. What is a Document in MongoDb?
2. What is Collection in MongoDb?
3. What are some fetures of MongoDb?
4. When to use MongoDb?
5. What are some of the advantages of MongoDB?
6. What type of DBMS is MongoDB?
7. What is the difference between MongoDB and MYSql?
8. Explain the Structure of ObjectID in MongoDB?
9. What is CRUD in MongoDB?
************Mongo DB Interview Questions************
Operator | Description | Operator | Description | |
---|---|---|---|---|
1 | $eq | Matches values that are equal to the given value | $gt | Matches if values are greater than the given value |
4 | $lt | Matches if values are less than the given value | $gte | Matches if values are greater or equal to the given value |
4 | $lte | Matches if values are less or equal to the given value. | $in | Matches any of the values in an array |
4 | $ne | Matches values that are not equal to the given value. | $nin | Matches none of the values specified in an arrray |
4 | $and | $not | ||
4 | $nor | $or |
Questions | Answer | |
---|---|---|
1 | Limit Data | const products = await cursor.limit(10).toArray(); |
3 | Map sort | {cart ?.sort((a, b) => a._id - b._id) .map((product) => ( ))} |
4 |