Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
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 29 30 31
Archives
Today
Total
관리 메뉴

개발자의 여의도 표류기

[Node.js] DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead. 본문

Trouble Shooting

[Node.js] DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead.

인수은 2023. 6. 26. 18:43

이런 에러가 뜨는 원인은 아래 코드에 있다.

 

/models/User.js

 

const userSchema = new mongoose.Schema({
  email: { type: String, required: true, unique: true },
  username: { type: String, required: true, unique: true },
  password: { type: String, required: true },
  name: { type: String, required: true },
  address: { type: String },
});

 

바로 unique: true 때문이다.
이게 오래된 방식이기 때문에 새로운 방식으로 설정해주어야 한다.

 

/db.js

 

mongoose.connect("DB_ADRESS", {
  useNewUrlParser: true,
  useUnifiedTopology: true,
  useFindAndModify: false,
  useCreateIndex: true,
});

 

useCreateIndex: true 설정을 해주면 새로운 버전을 쓰게 되고 경고가 나타나지 않게 된다.