본문 바로가기
[MongoDB] 데이터 파이프라인과 Lookup 집계 함수GROUP BY, SUM , AVG, COUNT$group, $sum, $avg, $count-------$lookup$matchSELECT, AS$project, $addFields, $set------[ {$match : {status : "A"}}, {$group : {"_id" : "$cust_id", total : {$sum : "$amount"}}, {$sort : {"total" : -1}]{ "cust_id" : "A", "amount" : 3}{ "cust_id" : "B", "amount" : 10}{ "cust_id" : "A", "amount" : 3}{ "_id" : "A", "total" : 6}{ "_id.. 2025. 6. 16.
[MongoDB] Cursor SQL의 ResultSet 개념 비교 요약개념MongoDB (cursor)SQL (ResultSet)정의쿼리 결과를 순차적으로 읽을 수 있는 객체쿼리 결과를 순차적으로 읽을 수 있는 객체동작 방식지연 평가(lazy evaluation)로 필요한 만큼 서버에서 데이터를 가져옴필요한 만큼 가져오거나, 전체를 메모리에 올림 (DBMS마다 다름)반복 처리.next(), .hasNext()로 순회.next(), .getXXX()로 순회예시db.collection.find() → cursor 반환SELECT * FROM table → ResultSet 반환클라이언트 전송MongoDB는 한번에 다 안 보내고, batch로 나눠서 전송SQL도 일부 드라이버는 페이징 처리 가능 MongoDB 예시jsconst curs.. 2025. 6. 16.
[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.