본문 바로가기
Back Side/DBMS > MongoDB

[MongoDB] syncIndexes() 이용해서 변경한 index 자동 적용시키기

by developerBeluga 2022. 7. 28.
728x90
반응형

 

 

예를 들어, name과 category에 index를 적용시켜줬습니다.

하지만 category는 필요 없어져서 삭제시켜준 후 다시 서버를 돌린다고 하면 어떻게 될까요?

예상대로라면 name만 index 적용이 되어야 합니다.

 

예상과 달리 name, category 모두 index 적용이 되어있었습니다.

방법을 몰라 robo3T에 직접 들어가서 매번 Index를 삭제하고 다시 서버를 돌렸죠.

이 행위가 매우 귀찮고 mongoDB에서 알아서 Index 자동 적용시켜주면 안되나 생각했습니다.

 

그러던 중 syncIndexes의 존재를 알게 되었습니다 ❗️

출처: https://thecodebarbarian.com/whats-new-in-mongoose-5-2-syncindexes

찾아보니 5.2.0 버전 이전에는 mongoose에서 지원을 해주지 않았다고 합니다.

하지만 5.2.0 이후부터는 synclndexes()를 프로덕션 데이터베이스에서 인덱스를 삭제할 수 있게 된 것입니다.

 

Mongoose 덕분에 MongoDB Index 적용을 쉽게 할 수 있게 되었습니다.

사용방법은 아주 간단합니다.

 

Model.syncIndexes()

mongoose의 schema 짜는 코드에 위 코드 한 줄을 포함시켜주시면 됩니다 👍

 

 

참고

https://thecodebarbarian.com/whats-new-in-mongoose-5-2-syncindexes

 

What's New in Mongoose 5.2.0: syncIndexes()

Mongoose released v5.2.0 to support MongoDB 4.0 and transactions this week. In addition to transaction support, Mongoose 5.2.0 introduces several important new features. The syncIndexes() function represents Mongoose's first steps toward providing a real i

thecodebarbarian.com

 

 

728x90
반응형

댓글