{"id":18862836,"url":"https://github.com/anshsinghsonkhia/project-lopipop-learning-mongodb","last_synced_at":"2026-01-19T15:10:47.269Z","repository":{"id":218967970,"uuid":"747800594","full_name":"AnshSinghSonkhia/project-lopipop-Learning-MongoDB","owner":"AnshSinghSonkhia","description":"Project Lopipop - Learning MongoDB with Express Generator","archived":false,"fork":false,"pushed_at":"2024-09-28T03:48:24.000Z","size":3342,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-04T20:51:19.130Z","etag":null,"topics":["express-boilerplate","express-generator","expressjs","mongodb"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/AnshSinghSonkhia.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-01-24T17:03:29.000Z","updated_at":"2024-09-28T03:48:22.000Z","dependencies_parsed_at":"2024-01-30T16:06:46.372Z","dependency_job_id":"25d9101d-7979-4dbe-864e-ef371a5b895c","html_url":"https://github.com/AnshSinghSonkhia/project-lopipop-Learning-MongoDB","commit_stats":null,"previous_names":["anshsinghsonkhia/project-lopipop"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AnshSinghSonkhia%2Fproject-lopipop-Learning-MongoDB","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AnshSinghSonkhia%2Fproject-lopipop-Learning-MongoDB/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AnshSinghSonkhia%2Fproject-lopipop-Learning-MongoDB/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AnshSinghSonkhia%2Fproject-lopipop-Learning-MongoDB/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AnshSinghSonkhia","download_url":"https://codeload.github.com/AnshSinghSonkhia/project-lopipop-Learning-MongoDB/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248888696,"owners_count":21178094,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["express-boilerplate","express-generator","expressjs","mongodb"],"created_at":"2024-11-08T04:35:53.095Z","updated_at":"2025-04-14T13:31:43.864Z","avatar_url":"https://github.com/AnshSinghSonkhia.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Let's Learn `MongoDB`\n\n- MongoDB is a DataBase.\n- There are 2 types of DataBase:\n    1. Relational DB\n        - MySQL\n        - SQL\n    2. Non-Relational DB\n        - MongoDB\n\n## Diagram\n\n\n| Code Side  | MongoDB Side |\n| :--------: | :-------:    |\n| DB Setup   | DB Formation |\n| Model      | Collection   |\n| Schema     | Documents    |\n\n# Concept of MongoDB\n\n- The `Storage` will have `Containers` of data of different apps.\n- Every app will have it's own `Container` to store its data.\n- Every `Container` is the DataBase of that one `app` or `Project`.\n\n## Example of the DataBase of Amazon.\n\n- The DataBase of Amazon will consist for the following `containers` of data:\n    - User DB\n    - Products DB\n    - Sales DB\n    - Admins DB\n    - \u0026 Many more.....\n- One whole DataBase of Amazon is divided into different parts (i.e., variety of data) known as `Model` in coding \u0026 `Collection` in DataBase. \n- We write code for `Model`, which makes `Collection` in DataBase.\n    - For Example:\n        - `Product Model` will be coded, which will create `Product Collection` in the DataBase.\n        - `User Model` will be coded, which will create `User Collection` in the DataBase.\n        - `Sales Model` will be coded, which will create `Sales Collection` in the DataBase.\n\n### Schemas in code will create Documents in DataBase.\n\n- `Schemas` in code will create `Documents` in DataBase.\n\n- The Data of all users is `Collection`.\n- The Data of ONE user in any collection is `Document`.\n\n# Setup Steps for MongoDB\n\n1. Install `MongoDB`\n    - Download from:\n    ```\n    https://www.mongodb.com/try/download/community\n    ```\n2. Install `MongooseJS`\n    ```shell\n    npm i mongoose\n    ```\n3. Require \u0026 Setup Connection\n\n    - Write this code in `routes/users.js`\n    ```js\n    // require mongoose\n    const mongoose = require(\"mongoose\");\n\n    // Set Connection between mongoose \u0026 mongoDB\n    mongoose.connect(\"mongodb://127.0.0.1:27017/DBname\");   // Use the name of your DB replacing \"DBname\". Whatever you wish to use as a name.\n\n    // This url is your device's localhost. MongoDB (by default) runs on port 27017\n    // Mongoose is connecting to the mongoDB running on the server at the LocalHost.\n    ```\n    - This creates database.\n\n4. Make Schemas\n\n    - Using `Schemas` you have tell, what will a `document` of the `collection` will look like.\n    - Use the below code in `routes/users.js`\n    ```js\n    // Creating Schemas\n\n    const userschema = mongoose.Schema({\n        username: String,\n        name: String,\n        age: Number\n    })\n\n    // name-of-value: Type-of-value\n    ```\n\n    - This tells, how every document will look. (Units of data)\n\n5. Create Model\n\n    ```js\n    // Creating Models\n\n    mongoose.model(\"userData\", userschema);    // \"userData\" is the name of collection created\n    ```\n\n    - It creates `collection` in DataBase.\n\n6. Export the Model\n\n    ```js\n    // Export Model\n\n    module.exports = mongoose.model(\"user\", userschema);\n    ```\n\n\u003e In the above example,\n\u003e `prcticekaro` is the database created.\n\u003e `userData` is a collection created inside the database.\n\u003e `userschema` defines the structure of data stored in every `document` of the collection.\n\n# Mongoose.js\n\nMongoose is a JavaScript object-oriented programming library that creates a connection between MongoDB and the Node.js JavaScript runtime environment.\n\n# CRUD - Create Read Update Delete\n\n## Create\n\n\u003e import `user.js` in `index.js` to bring all the information about database, collections, schema.\n```js\n// import users.js as userModel\nconst userModel = require(\"./users\");\n``` \n\n\u003e Create documents in `index.js`\n```js\nrouter.get('/create', async function(req,res){\n  await userModel.create({\n    username: \"ansh07\",\n    age: 20,\n    name: \"Ansh Singh Sonkhia\"\n  });\n  res.send(\"Successfullllly Created\")\n  // This \"userModel.create\" is asynchronoud JS, So, will go into the side stack \u0026 will be implemented after the completion of main stack Synchronous code. That's why we use \"await\" before it.\n  // nhi toh user ko pehle hi message chala jayega\n});\n```\n\n- After Creation of this user, mongoDB will return the created user, which we can save.\n```js\nrouter.get('/create', async function(req,res){\n  const createduser = await userModel.create({\n    username: \"ansh07\",\n    age: 20,\n    name: \"Ansh Singh Sonkhia\"\n  });\n  res.send(\"Successfullllly Created\")\n  res.send(createduser);\n});\n```\n\n## Read\n\n```js\nrouter.get(\"/allusers\", async function(req,res){\n  let allusers = await userModel.find();\n  res.send(allusers);\n});\n```\n\n- `.find` is used to find\n- `.findOne` is used to find one user\n\n```js\nrouter.get(\"/allusers\", async function(req,res){\n  let allusers = await userModel.findOne({username: \"harshita\"});\n  res.send(allusers);\n});\n```\n\n# Update\n\n\n# Delete\n\n```js\nrouter.get(\"/delete\", async function(req,res){\n  let deleteduser = await userModel.findOneAndDelete({\n    username: \"harsh\"\n  });\n  res.send(deleteduser);\n});\n```\n\n## Client-Server Diagram\n\n\n|  Client  |   Server   |\n| :--------: | :-------:  |\n| Cookie   | Session |\n\n- When you have to save data on `server` - use `session`.\n- When you have to save data on `Client's frontend'` - use `Cookie`.\n\n----\n- Data saved in the server is more secured than the data saved in the client's cookie.\n\n# Sessions\n\n- To use `sessions` - install this package:\n```shell\nnpm i express-session\n```\n\n- Write this code in `app.js`\n```js\nvar session = require('express-session');\n\napp.use(session({\n  resave: false,  // don't save again, if the value of session is NOT changed.\n  saveUninitialized: false,   // Don't save any data, which is NOT named.\n  secret: \"kuchBhiRandomSecretCodeLikhDoo\"    // A secret string, on the basis of which our data will be encrypt.\n}));\n```\n\n- You can create session in the routes with any name of your choice, whenever someone visits that route.\n```js\nrouter.get(\"/\", function (req, res) {\n  req.session.anyname = \"heloo\";\n  res.render(\"index\");\n});\n```\n### Example Use-Case\n- So, if you want anyone to get banned, when he visits any route...\n```js\nrouter.get(\"/\", function (req, res) {\n  req.session.banned = true;\n  res.render(\"index\");\n});\n```\n\n\u003e You can use any name for session - `banned`, `ban`, `noban`, `lemon`, `bmw`, etc.... \n\n\n### If a session is created in any route, it can be checked in all other routes.\n\n```js\nrouter.get(\"/check-ban\", function (req, res) {\n  console.log(req.session);\n  res.send(\"You have beeeeeeeeeeen Banned\");\n\n  if(req.session.banned === true){\n    res.send(\"You have beeeeeeeeeeen Banned\");\n  }\n  else{\n    res.send(\"not banned\");\n  }\n});\n```\n\n### If the server is restarted...\nIf the server is restarted or restarted by `nodemon`\nThen, the session will be deleted.\n\n### How to delete session?\n\n```js\nrouter.get(\"/remove-ban\", function (req, res) {\n  req.session.destroy(function(err){\n    if (err) throw err;\n    //console.log(err);\n    res.send(\"Ban Removed Successfullyy\")\n  })\n});\n```\n\n# Cookie\n\n- install package `cookie-parser`\n\n- use code:\n```js\nvar cookieParser = require('cookie-parser');\napp.use(cookieParser());\n```\n\n## How to use Cookie\n\u003e Cookie is set on the frontend. So, sent in `response`\n\n```js\nrouter.get(\"/\", function (req, res) {\n  res.cookie(\"age\", 25);\t// cookie(\"cookie-name\", cookie-value)\n  res.render(\"index\");\n});\n```\n\n## How to read Cookie?\n\n- The cookie is set on the browser of client.\n- And, we have to read it on server.\n- So, we have to `request` it.\n\n```js\nrouter.get(\"/read\", function (req, res) {\n\tconsole.log(req.cookies);\n\tconsole.log(req.cookies.age);\t// to get direct data of cookie named \"age\"\n\tres.send(\"check\");\n});\n```\n\n## How to delete Cookie?\n\n```js\nrouter.get(\"/delete\", function (req, res) {\n\tres.clearCookie(\"age\");\n\tres.send(\"Cookie Cleared Successfullyy\");\n});\n```\n\n## EndGame-1 Completed ","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanshsinghsonkhia%2Fproject-lopipop-learning-mongodb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fanshsinghsonkhia%2Fproject-lopipop-learning-mongodb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanshsinghsonkhia%2Fproject-lopipop-learning-mongodb/lists"}