본문 바로가기

MongoDB

몽고DB 설치부터 ~서버에서 db 넣어보기

server.js가 (서버가 ) db를 컨트롤 할 수 있게 하기 


0. ip 는 what is my id on goggle

 

1. mongoDB atlas 접속 후 개인 ip 여러 설치 조건 만족 후 Databases 에 connect 클릭!!

 

2. url 복사하여 연결할 준비하기 

 

3. npm install mongodb  설치

 

 

4.몽고db 와 서버를 연결 

const MongoClient = require('mongodb').MongoClient;

 

MongoClient.connect('아까 챙겨온 접속URL', function(에러, client){
  if (에러) return console.log(에러);
  //서버띄우는 코드 여기로 옮기기
  app.listen('8080', function(){
    console.log('listening on 8080')
  });
})

 

  

  5. db 사용

var db;
MongoClient.connect('접속URL', { useUnifiedTopology: true }, function (에러, client) {
	if (에러) return console.log(에러)
	db = client.db('todoapp');

	app.listen(8080, function () {
		console.log('listening on 8080')
	});
});

 

6. db 의 자료 넣기 

app.listen (8080) 선언 위에!!

db.collection('post').insertOne({이름:'John', _id: 100} ,function(error,result){
		console.log('저장완료')
	})

'MongoDB' 카테고리의 다른 글

html 데이터 삭제 방법  (0) 2021.10.06
db에 아이디 달기(영구 결번의 중요성)  (0) 2021.10.05
db 내용 html에 보여주기  (0) 2021.10.05
고객의 post 요청 db에 담기  (0) 2021.10.04