본문 바로가기
[MongoDB] 정렬과 페이징 db.users.find().sort({age : 1, name : -1}).skip(10).limit(10)db.users.aggregate([ {$sort : {age : -1}, {$skip : 20}, {$limit : 10}]){ "_id": ObjectId("..."), "name": "Alice", "age": 29, "email": "alice@example.com", "address": { "city": "Seoul", "zip": "04524" }, "hobbies": ["reading", "music", "swimming”],},// 추가적인 필드가 더 있다고 가저}// project 1은 포함할 필드만 선언db.users.aggregate(.. 2025. 6. 16.
[MongoDB] 주요 쿼리 연산자 $eq : 같다는 조건$ne : 같지 않다는 조건$gt, $gte : 특정 값보다 크다는 조건 (이상, 초과)$lt, $lte : 특정 값보다 작다는 조건 (이하, 미만)$in : 여러 값 중에서 하나와 일치$nin : 여러 값 중에서 어떤 것과도 일치하지 않음 (not in)db.users.find({age : {$gte : 25}})--------$and : 모든 조건이 참$or : 하나라도 참$not : 조건이 거짓$nor : 모든 조건이 거짓// -> 나이가 25이거나 city가 Seoul인 docdb.users.find( {$or : [ {age : {$gte : 25}}, {city : "Seoul"} ]})-----------$exists : 필드 존재하냐.. 2025. 6. 16.
[MongoDB] CRUD와 Upsert insertOne, insertManydb.users.insertOne({name : "Alice", age : 25})db.users.insertMany( {name : "Alice", age : 25}, {name : "test", position : "dev"} )------------find, findOnedb.users.find()db.users.find({age : {$gte : 25}})db.users.findOne({name : "Alice"})--------------updateOne, updateManydb.users.updateOne({name : "Alince"}, {$set : {age : 30}})db.users.updateMany({city: "Busan"}, .. 2025. 6. 16.
[MongoDB] 인덱스 설계 법칙과 쿼리 튜닝을 위한 실행 계획 분석 db.orders.createIndex({userId : 1, status : 1, createdAt : -1})db.orders.find({userId : 123, status: "paid"}).explain("executionstats")------IXSCAN : 인덱스 스캔이 발생 (제대로 사용)COLLSCAN : 전체 컬렉션을 스캔 (인덱스를 미사용)FETCH : 인덱스에서 찾은 문서를 읽었다.SROT : 인덱스를 사용해서 정렬으 하지 못했다.nReturned : 실제로 반환되는 문서의 수totalkeysExamined : 인덱스에서 검사한 엔트리의 갯수 (작을 수록 효율적)totalDocsExamined : 실제 문서를 몇개 읽었는지 (작을 수록 효율적)executionTimeMillis : l.. 2025. 6. 16.
[MongoDB] 다양한 인덱스 {_id : 1, tags : ["mongodb", "nosql", "index"]}{_id : 2 , tages : ["database", "index"]}db.users.createindex({tags : 1})"database" -> _id : 2"index" : -> _id : 1, _id : 2"mongodb" -> _id : 1"nosql" -> _id : 1--------db.users.createIndex({"creatdAt"}, {expireAfterSeconds : 3600})---------db.users.createindex({title : "text", content : "text"})-> db.users.find({$text : {$search : "mongodb index"}) 2025. 6. 16.
[MongoDB] index Level 0 (Root)+-----------------------------+| [30 | 60] | 루트 노드 (최상위)+-----------------------------+ / | \ / | \Level 1 Level 1 Level 1+-----------+ +-----------+ +-----------+| [10|20] | | [40|50] | | [70|80|90]|+-----------+ +-----------+ +-----------+ / \ / \ / | \ / \ .. 2025. 6. 16.