반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- tabBarStyle
- useMemo
- mongoDB
- react native
- 갤럭시 스킨
- useCallback
- redux/toolkit
- 이미지 배경 지우기
- useEffect
- bottom tabBar
- ReactQuery
- gitpages
- useRef
- 포트폴리오 사이트
- Drawer
- listeners
- MonGo
- 리액트네이티브
- iTerm2
- bottom tabs
- useState
- 에뮬레이터 스킨
- oh-my-zsh
- 리액트 쿼리
- rembg
- nodejs
- react
- reactnative
- Hook 함수
- Stack
Archives
- Today
- Total
프론트엔드 개발자의 개발 놀이터
MongoDB와 NodeJS 연동하기 본문
반응형
1. MongoDB 모듈
NodeJS 드라이버 모듈로 MongoDB를 자체 콘솔에서 사용하는 것처럼 명령어를 그대로 사용할 수 있습니다.
npm i mongodb
- mongodb nodejs 연결
// MongoClient : MongoDB에 연결해서 요청을 보낼 클라이언트 객체
const client = require('mongodb').MongoClient
// useUnifiedTopology : 통합모드
client.connect(url, {useUnifiedTopology:true}, 콜백 함수);
로컬 pc에 연결할 때
url : mongodb://127.0.0.1:27017
다른 pc에 연결하고 싶을 때
url : mongodb://ip주소:port번호/데이터베이스
- 데이터베이스 연결
const db객체명 = 연결객체.db('데이터베이스명');
- 컬렉션 객체
const 컬렉션객체명 = db객체명.collection('컬렉션명');
- 데이터 삽입
컬렉션객체명.insertOne(객체, 콜백함수)
컬렉션객체명.insertMany([객체1, 객체2, ...], 콜백함수)
// insert 카운트 : result.insertedCount
- 데이터 수정
컬렉션객체명.updateOne(객체, {$set:{수정할 객체}}, 콜백함수)
// update 카운트 : result.modifiedCount
- 데이터 삭제
컬렉션객체명.deleteOne(객체, 콜백함수)
// delete 카운트 : result.deletedCount
- 데이터 검색
컬렉션객체명.find(객체).toArray((err, result) => {
수행할 문장(result 활용)
});
'MongoDB' 카테고리의 다른 글
MongoDB 문법 (0) | 2024.02.02 |
---|---|
MacOS MongoDB 설치 및 설정 (0) | 2024.02.02 |