티스토리 뷰
mongoDB = NoSQL Databass
mongoose = mongoDB와 자바스크립트를 연결 시켜줌
장점 - 적은 규칙과 절차가 간편하게 작업이 가능한 데이터 베이스 이다.
1. mongoDB 다운로드
MongoDB
MongoDB Atlas Cloud-hosted MongoDB service on AWS, Azure, and GCP. Deploy, operate, and scale a MongoDB database in just a few clicks. Trusted by thousands of companies, from startup to enterprise “With MongoDB Atlas, time is being spent on development and
www.mongodb.com
2. 실행해보기
vsc 터미널에서 mongod => mongo 실행
실행이 안된다면 아래 과정 실행
1. [내컴퓨터] 우클릭 > [고급시스템설정] > [환경변수(N)]
2. 시스템변수 리스트 중 'Path'를 선택하고 편집을 선택한 후 'Mongodb의 설치경로₩bin'을 추가
3. vscode 재실행 후 터미널에 'mongod'입력
3. db.js 에 연결시키기
import mongoose from "mongoose";
mongoose.connect("mongodb://localhost:포트번호/Databass이름", {
useUnifiedTopology: true,
useNewUrlParser: true
});
const db = mongoose.connection;
const handleOpen = () => console.log("Connected to DB");
const handleError = error => console.log(`error on DB connection:${error}`);
db.once("open", handleOpen);
db.on("error", handleError);
useNewUrlParser, useUnifiedTopology는 이걸 안적고 실행해보면
mongoose에서 자꾸 에러를 뱉는데(아래같은)
즉 이놈은 사용되지 않고, 신버전에서 제거될꺼다. 사용하려면 추가해라. 이런느낌이다.
경고가 자꾸 뜨니까 추가해준다.
export 되어질 mongoose.connection 를 만들고 거기에
서버가 연결되었을때 메세지를 출력하는 함수와 에러가 났을때의 메세지출력 함수를 만들어
.once와 .on 으로 적어준다.
여기서 once와 on 은 mongoose 의 Quick start 가이드에 설명되어있다
.https://mongoosejs.com/docs/index.html
Mongoose v5.9.2: Getting Started
Getting Started First be sure you have MongoDB and Node.js installed. Next install Mongoose from the command line using npm: $ npm install mongoose Now say we like fuzzy kittens and want to record every kitten we ever meet in MongoDB. The first thing we ne
mongoosejs.com
'Study > script.js' 카테고리의 다른 글
[Node.js] 회원가입 구현(로그인,로그아웃,회원가입,passport, Mongo Store) (0) | 2020.03.23 |
---|---|
[error]node_modules\path-to-regexp 에러(Cannot read property 'length' of undefined) (0) | 2020.03.19 |
[string]문자열 정렬(split, sort, reverse) (0) | 2020.02.26 |
[string] split - 문자열 분할 (0) | 2020.02.26 |
[express] View - PUG (0) | 2020.02.24 |