Mongo DB 연결 / Mongo DB MODEL & Schema / git 설치 / #3~#5

해버니·2022년 7월 10일
1

노드 & 리액트

목록 보기
2/9
post-thumbnail

몽고 DB 연결

  1. 몽고 DB 사이트 가기
  2. 회원 가입하기
  3. CLUSTER(클러스터)를 만들기
  4. 몽고DB 유저 생성 -> 아이디 비번 꼭 기억해야 함
  5. Mongoose 다운로드 -> npm install mongoose --save
  6. App에 MongoDB 연결하기
  7. 잘 연결 되었는지 확인 !




Mongo DB 연결 -> cluster 만들기

https://www.mongodb.com/

mongoDB유저 생성하기


connect 눌러서 연결하기


Mongoose란?

간단하게, 몽고 DB를 편하게 쓸 수 있는 Object Modeling Tool이다.


"npm install mongoose --save" 입력해서 다운 받기



연결 성공~


문제


하지만 여기서 에러가 났다

MongoParseError: options usecreateindex, usefindandmodify are not supported

MongoParseError: options usecreateindex, usefindandmodify are not supported
//이 줄 주석처리 하기! 

핑크색으로 네모친 부분을 주석 처리 하면 문제가 해결된다.






Mongo DB MODEL & Schema

우리는 어떤 웹 사이트에 가면 회원가입과 로그인을 한다.
회원가입을 할 때 우리가 쓴 username이나 사는 곳, 나이 등등을 입력한다고 치면 그것들이 userDatabase에 들어가게 된다.
그래서 그 user와 관련된 Data들을 보관하기 위해서 userModel, userSchema를 만들어 볼 것이다.


모델은 스키마를 감싸주는 역할

스키마는 세부적으로 지정하는 것

A Mongoose model is a wrapper on the Mongoose schema.
A Mongoose schema defines the structure of the document, default values, validators, etc.. whereas a Mongoose model provides an interface to the database for creating, querying, updating, deleting records, etc.

상품에 관련된 글을 작성한다고 하자.
그러면 그 글을 작성한 사람이 누구인지, 작성할 때의 포스트 이름이 뭔지 (type이 뭔지, string으로 한다면 최대 길이가 어느 정도까지인지), 어떤 설명이 적혀져 있는지
이런 걸 지정해주는 게 스키마를 통해서 할 수 있는 것이다.



require이란??

외부 모듈을 가져올 수 있는 기능




trim은 뭐야?

노란색 네모 박스 안에 있는 것처럼 띄어쓰기를 없애주는 것!!!



User.js

const mongoose = require('mongoose');

const userSchema = mongoose.Schema({
    name: {
        type: String,
        maxLength: 50
    },
    email: {
        type: String,
        trim: true,
        unique: 1
    },
    password: {
        type: String,
        minlength: 5
    },
    lastname: {
        type: String,
        maxlength: 50
    },
    role: {	 	// 관리자, 일반 유저 나누기 위해서
        type: Number,
        default: 0
    },
    image: String,
    token: {
        type: String
    },
    tokneExp: { 	// token의 유효 기간 
        type: Number
    }
})

const User = mongoose.model('User', userSchema)

module.exports = { User } 	// 다른 곳에서도 쓸 수 있게






git 저장소 만들기

GIT이 무엇 인가요?

Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.

분산 버전 관리 시스템이라고 한다!
여러명이 한 사이트 내에서 코드를 짜고 있다고 하자.
어떤 사람은 user에 관련한 것, 또 어떤 사람은 상품에 관련한 것을 만들고 있다.
두 사람이 다 만든 다음에 깃을 통해서 합칠 수 있다.
어떤 사람이 어떤 것을 했는지 볼 수 있는, 관리할 수 있는 시스템이다.



  1. git --version (설치 되어 있는지 확인)
  2. git init
  3. git status
  4. gitgnore node_module
  5. git status -> node_module 없어짐
  6. git add .
  7. git commit -m "커밋메세지 자유롭게 적기"
  8. (모든 정보를 지워주고 싶다면) git rm --cached 파일이름 -r





먼저 깃 다운로드를 받는다.
https://git-scm.com/downloads

버전 관리를 해야하기 때문에 이 깃을 저장소에 만들어주기 위해서 “git init” 입력

The git init command creates a new Git repository.
Executing git init creates a .git subdirectory in the current working directory, which contains all of the necessary Git metadata for the new repository.





"git status"는 git의 현재 상태를 볼 수 있는 것

The git status command displays the state of the working directory and the staging area. It lets you see which changes have been staged, which haven't, and which fiels aren't being tracked by Git. Status output does not show you any information regarding the committed project history. For this, you need to use git log.





Working Directory가 아무것도 안 한 상태를 뜻한다.

Staging Area는 Git에만 있는 개념이라고 한다.
git repository에 넣기 전에 대기시켜주는 곳이라고 보면 된다.



Staging Area
-> Instead of committing all of the changes you've made since the last commit, the stage lets you group related changes into highly focused snapshots before actually committing it to the project history.

The git add command adds a change in the working directory to the staging area. It tells Git that you want to include updayes to a particular file in the next commit. However, git add doesn't really affect the repository in any significant way.

The "commit" command is used to save your changes to the local repository.

The git push command is used to upload local repository content to a remote repository. Pushing is how you transfer commits from your local repository to a remote repo.






"git add ." 를 입력하면 새로운 파일들이 생긴다.





특정 파일을 제외하고 저장소에 올리는 방법

node_modules 폴더는 깃에 올리지 않아도 된다.

(왜냐하면 너무 크기가 크기때문에? 올리지 않아도 된다고 말씀하셨다.)



.gitgnore 파일을 하나 만들고 “node_modules”라고 적어준다.
그럼 node_modules 파일을 제외하고 git에 올릴 수 있게 된다.





저장소에 올리는 방법


git commit -m “원하는 내용 적기”




0개의 댓글