728x90
MongoDB is a NO-SQL database that stores JSON documents. Mongoose is a npm package used to control MongoDB with Javascript, creating schemas.
- SQL : saving all data across tables
- NO-SQL : saving all data within one record
MongoDB has a merit combinated with Javascript that its format is JSON, which is Javascript Object Notation.
Web - Server - Database
- Web client : HTML, CSS, Javascript
- Web server : Node.js, Mongoose(npm package)
- Database : JSON
Installation
Deployment with MongoDB can be served with
- Atlas : MongoDB as a service
- On-premises : Local MongoDB => install MongoDB Compass
- Mobile & Edge : Realm mobile database. Lightweight data storage for mobile and edge
Connection
- Create an account in MongoDB website
- Choose a plan : 1) serverless 2) dedicated 3) shared(free, 1 per account)
- Set database username and password, which will be used in Mongoose.
- Set other configurations for the MongoDB, like IP Access List. One of the common reasons of DB connection failure is not to include your IP address.
- Get your MongoDB Atlas URI and set environment variables.
- Require Mongoose/Express in your Javascript file and connect database like below.
// case 1 : using process.env to import MONGO_URI
const mongoose = require('mongoose')
const MONGO_URI = process.env['MONGO_URI'] // In .env file, MONGO_URI="your uri"
mongoose.connect(MONGO_URI, {})
.then(()=>{ console.log("MongoDB connected") })
.catch((err)=>{ console.log(err) })
// case 2 : using modules to import MONGO_URI
module.exports = { MONGO_URI : "your uri" } // file name : key.js
require('./key')
mongoose.connect(MONGO_URI, {})
.then(()=>{ console.log("MongoDB connected") })
.catch((err)=>{ console.log(err) })
728x90
'창고 3 > [Dev] My Readme' 카테고리의 다른 글
몽고 DB 기초 3 (0) | 2022.01.06 |
---|---|
몽고 DB 기초 2 (0) | 2022.01.05 |
git commit 취소하는 법, git push 취소하는 법, git 폴더명 변경하기 (0) | 2022.01.04 |
git pull request 단계별 연습하기 (0) | 2022.01.03 |
운영체제 기초 2 (0) | 2021.12.27 |
댓글