예를 들어, name과 category에 index를 적용시켜줬습니다.
하지만 category는 필요 없어져서 삭제시켜준 후 다시 서버를 돌린다고 하면 어떻게 될까요?
예상대로라면 name만 index 적용이 되어야 합니다.
예상과 달리 name, category 모두 index 적용이 되어있었습니다.
방법을 몰라 robo3T에 직접 들어가서 매번 Index를 삭제하고 다시 서버를 돌렸죠.
이 행위가 매우 귀찮고 mongoDB에서 알아서 Index 자동 적용시켜주면 안되나 생각했습니다.
그러던 중 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
'Back Side > DBMS > MongoDB' 카테고리의 다른 글
[MongoDB] 대량의 데이터 처리 프로그래밍 모델 (1) 맵리듀스 map-reduce (0) | 2022.11.08 |
---|---|
[MongoDB] Robo3T Failed to load documents 해결 방법 - Studio 3T 설치 (0) | 2022.08.30 |
[MongoDB] Transaction 트랜잭션 개념부터 실습까지 (3) - NodeJS에 사용하기 (0) | 2022.07.13 |
[MongoDB] Transaction 트랜잭션 개념부터 실습까지 (2) - 로컬에서 Replica Set 구성하기 (0) | 2022.07.12 |
[MongoDB] Error: couldn't connect to server 127.0.0.1:27017 오류해결 (1) | 2022.07.08 |
댓글