This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 2dsphere 인덱스 생성 | |
| db.places.createIndex({ location: "2dsphere" }); | |
| // 근처 장소 쿼리하기 | |
| db.places.find({ | |
| location: { | |
| $near: { | |
| $geometry: { type: "Point", coordinates: [-73.9667, 40.78]}, | |
| $maxDistance: 500 | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 뉴욕시의 특정 좌표 예시 | |
| [-73.97, 40.77] | |
| // 좌표 데이터에 2d 인덱스를 생성하는 예시 | |
| db.collection.createIndex({ loc: "2d" }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 사용자 이름으로 검색 최적화 | |
| db.users.createIndex({ "username": 1 }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 이름은 오름차순, 성은 내림차순으로 정렬하는 복합 인덱스 | |
| db.users.createIndex({ "firstname": 1, "lastname": -1 }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 특정 태그가 포함된 문서를 빠르게 조회해야 하는 케이스 | |
| db.collection.createIndex({ "tags": 1 }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // tags 배열 필드와 rating 필드를 포함하는 문서 컬렉션에서 복합 다중 키 인덱스를 생성 | |
| // 태그는 배열 형태의 데이터이고, 평점은 단일 숫자 값을 가지는 필드 | |
| db.collection.createIndex({ "tags": 1, "rating": -1 }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // description 필드에 text 인덱스 생성 | |
| db.post.createIndex({ "description": "text" }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 문서의 모든 필드에 인덱스 생성 | |
| db.users.createIndex({ "$**": 1 }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 각 로그 항목을 생성된 지 1시간 후에 자동 제거 | |
| db.logs.createIndex({ "createdAt": 1 }, { expireAfterSeconds: 3600 }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 새로운 사용자가 가입할 때마다 사용자 이름이 고유한지 확인 | |
| db.users.createIndex({ "username": 1 }, { unique: true }) |
NewerOlder