-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpractice.js
More file actions
56 lines (46 loc) · 1.75 KB
/
practice.js
File metadata and controls
56 lines (46 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// server/models/Profile.js
// Schema - attribute that validate data storing
const mongoose = require("mongoose");
const Profile = new mongoose.Schema({
// Mongoose provide lot of prebuilt tools for validation that makes our data more consistence and enhance data integrations
firstName: { type: String, default: "", trim: true },
lastName: { type: String, default: "", trim: true },
age: { type: Number, default: 0 },
team: { type: String, default: "", trim: true },
position: { type: String, default: "", trim: true },
});
module.exports = mongoose.model("Profile", Profile);
// Data querying is done in controller
const express = require("express");
const router = express.Router();
const Profile = require("../models/Profile.js");
router.get("/profile", (req, res) => {
Profile.find() // Query for every data in our mongoDB Profile collection
.then((profiles) => {
// success Block
res.json({
confirmation: "success",
data: profiles,
});
})
.catch((err) => {
// error Block
res.status(404).json({ confirmation: "fail", message: err.message });
});
});
module.exports = router;
// In database I have to learn about primary and foreign key and its equivalent in MongoDB as _id and reference
// reference in mongoDB is useful for better schema design
// $lookup -- to join different collections in mongoDB Database
// $populate -- alternative for $lookup in mongoose ODM used for referencing collections in mongoDB Database
// aggregation query in Mongoose for dabase computation
/*
** Important Node concepts
-- HTTP
-- EVENT LOOP
-- EVENT EMITTER
-- NODE STREAM -- To work with bigger size files
// Command to remove git tracking from a repo/folder
rmdir -Force -Recurse .git
rd /s /q .git
*/