티스토리 뷰
⛳️ 1. index 조회
locations라는 collection에 생성한 index들을 조회하고 싶을 때 .getIndexes()
> db.locations.getIndexes()
아래처럼 index 종류가 출력된다.
[
{ v: 2, key: { _id: 1 }, name: '_id_' },
{
v: 2,
key: { pos: '2dsphere' },
name: 'pos_2dsphere',
'2dsphereIndexVersion': 3
},
{
v: 2,
key: { position: '2dsphere' },
name: 'position_2dsphere',
'2dsphereIndexVersion': 3
}
]
나같은 경우는 pos라는 field가 없는데 2dsphere index가 생성되어 있었고 이 때문에 geoNear query에서 에러가 발생했다.
⛳️ 2. index 삭제
(1) index 선택해서 삭제
.dropIndex()
를 이용해서 index를 삭제하자
> db.collection.dropIndex(index)
index 안에는 key 값을 적어주면 된다.
나 같은 경우는
{
v: 2,
key: { pos: '2dsphere' },
name: 'pos_2dsphere',
'2dsphereIndexVersion': 3
},
index를 삭제하고 싶으므로 { pos: '2dsphere' }
를 index로 입력한다.
> db.locations.dropIndex({pos: '2dsphere'})
아래처럼 뜨면 index 삭제 성공
{ nIndexesWas: 3, ok: 1 }
(2) collection의 모든 index 삭제
만약 모든 index를 일괄 삭제하고 싶으면 db.collection.dropIndexes()
을 입력한다.
> db.collection.dropIndexes()
출처 : https://www.mongodb.com/docs/manual/tutorial/manage-indexes/
Manage Indexes — MongoDB Manual
Docs Home → MongoDB ManualThis page shows how to manage existing indexes. For instructions on creating indexes, refer to the specific index type pages.A sharded collection has an inconsistent index if the collection does not have the exact same indexes (
www.mongodb.com
'Database > mongoDB' 카테고리의 다른 글
[mongoDB/Error] There is more than one 2dsphere index; unsure which to use for $geoNear (0) | 2022.03.27 |
---|---|
[mongoDB/linux] mongo db 설치 (0) | 2022.03.14 |