출처: https://bumcrush.tistory.com/182 [맑음때때로 여름]

상세 컨텐츠

본문 제목

connect to mongoose

nodejs

by 장동균 2020. 9. 6. 17:19

본문

mongoosejs.com/docs/connections.html

 

Mongoose v5.10.3: Connecting to MongoDB

You can connect to MongoDB with the mongoose.connect() method. mongoose.connect('mongodb://localhost:27017/myapp', {useNewUrlParser: true}); This is the minimum needed to connect the myapp database running locally on the default port (27017). If connecting

mongoosejs.com

mongoosejs.com/docs/api.html#model_Model.findByIdAndUpdate

 

Mongoose v5.10.3: API docs

 

mongoosejs.com


const mongoose = require("mongoose");
const PORT = process.env.PORT || 5000;
const config = require("./config/dev");

const connect = mongoose
  .connect(config.mongoURI, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
    useFindAndModify: false,
  })
  .then(() => console.log("MongoDB Connected..."))
  .catch((err) => console.log(err));

기본적인 몽구스 connect

 

  • useNewUrlParser - The underlying MongoDB driver has deprecated their current connection string parser. Because this is a major change, they added the useNewUrlParser flag to allow users to fall back to the old parser if they find a bug in the new parser. You should set useNewUrlParser: true unless that prevents you from connecting. Note that if you specify useNewUrlParser: true, you must specify a port in your connection string, like mongodb://localhost:27017/dbname. The new url parser does not support connection strings that do not have a port, like mongodb://localhost/dbname.

 

  • useFindAndModify - True by default. Set to false to make findOneAndUpdate() and findOneAndRemove() use native findOneAndUpdate() rather than findAndModify().

 

 

공식문서의 내용들

 

useNewUrlParser, useUnifiedTopology 이 두개는 뭔지도 모르고 써야 한다해서 써왔던 것들인데 이번에 공식 문서를 보고 왜써야하는지를 알게 되었는데, 사실상 정확히 이해했냐고 물어보면 잘 모르겠다...

 


useFindAndModify 이 친구를 이번에 처음 써봤다.  

 

Post.findByIdAndUpdate 이 method를 사용하는데 자꾸만

 

DeprecationWarning: Mongoose: `findOneAndUpdate()` and `findOneAndDelete()` without the `useFindAndModify` option set to false are deprecated. See: https://mongoosejs.com/docs/deprecations.html#findandmodify DeprecationWarning: collection.findAndModify is deprecated. Use findOneAndUpdate, findOneAndReplace or findOneAndDelete instead.

 

이런 식의 오류가 뜨는 것이다. 에러의 내용을 이해해보자 다짐했건만 또 그새를 못 참고 바로 구글링 했다. 해결 방법은 다행이도 useFindAndModify 이 한줄만 추가하면 되는 단순한 이슈였다.

 

에러 메시지를 이해하는 능력이 필요하다. 

관련글 더보기

댓글 영역