connect 클릭
mongodb user 추가
choose a connection method 클릭
connect your application 클릭
connection string only 부분 복사
npm install mongoose --save
mongoose(몽고db를 편하게 쓸 수 있는 객체모델링툴) 설치
그러면 index.js의 scripts에 "mongoose" : 가 추가됨
const mongoose = require('mongoose')
mongoose.connect('mongodb+srv://<user>:<password>@XXX.XXX.mongodb.net/?retryWrites=true&w=majority', {
useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex: true, useFindAndModify: false
}).then(() => console.log('MongoDB Connected ...'))
.catch(err => console.log(err))
index.js에 추가
<user>부분에 아까 user 추가했을 때 적은 user명 입력, <password>부분에 적은 비밀번호 입력
(node:22619) [MONGOOSE] DeprecationWarning: Mongoose: the `strictQuery` option will be switched back to `false` by default in Mongoose 7. Use `mongoose.set('strictQuery', false);` if you want to prepare for this change. Or use `mongoose.set('strictQuery', true);` to suppress this warning. (Use `node --trace-deprecation ...` to show where the warning was created)
다 적고 실행하면 이 에러가 뜨는데 mongoose.set('strictQuery', false) 나 mongoose.set('strictQuery', true)를 추가해도 계속 뜬다
=> 검색해보니까 mongoose.set('strictQuery', false); 이 줄을 mongoose.connect 윗 줄로 올려야 warning이 안 뜬다!
+) 하지만 다음 강의에서 연결해보려니까
MongoParseError: options usecreateindex, usefindandmodify are not supported
at parseOptions (/Users/xxx/node_js/boiler_plate/node_modules/mongodb/lib/connection_string.js:283:15)
at new MongoClient (/Users/xxx/node_js/boiler_plate/node_modules/mongodb/lib/mongo_client.js:46:63)
at /Users/xxx/node_js/boiler_plate/node_modules/mongoose/lib/connection.js:802:16
at new Promise (<anonymous>)
at Connection.openUri (/Users/xxx/node_js/boiler_plate/node_modules/mongoose/lib/connection.js:799:19)
at /Users/xxx/node_js/boiler_plate/node_modules/mongoose/lib/index.js:411:10
at /Users/xxx/node_js/boiler_plate/node_modules/mongoose/lib/helpers/promiseOrCallback.js:41:5
at new Promise (<anonymous>)
at promiseOrCallback (/Users/xxx/node_js/boiler_plate/node_modules/mongoose/lib/helpers/promiseOrCallback.js:40:10)
at Mongoose._promiseOrCallback (/Users/xxx/node_js/boiler_plate/node_modules/mongoose/lib/index.js:1285:10) {
[Symbol(errorLabels)]: Set(0) {}
위의 오류가 뜨면서 연결이 안됐는데 몽고DB 6이상부터는 usecreateindex, usefindandmodify를 지원하지 않는다고 한다
그래서 두 개를 지워주니 연결 된다!
'웹 > Node.js' 카테고리의 다른 글
[MongoDB] 비밀 설정 정보 관리 (0) | 2023.02.25 |
---|---|
[MongoDB] BodyParser, Postman, 회원가입 기능 (1) | 2023.02.24 |
[MongoDB] model과 schema 설정 (0) | 2023.02.22 |
[node.js] nodemon (0) | 2023.02.21 |
[node.js] node.js 시작하기 (0) | 2023.02.21 |
댓글