본문 바로가기
[MongoDB] 실전 쿼리 작성2 [ // 특정 필드만 include, exclude {$project: { title : 1, _id : 0 // _id는 자동 생성되는데 쓰고 싶지 않으면 0으로 추가 }}, // 특정 필드만 include, exclude // 유지보수 가독성이 떨어짐. 좋은 형태는 아님 (프로시저 유사) // 쿼리를 가져와서 클라이언트 레벨에서 처리하는 것이 좋음 {$project: { title : 1, year : 1, genres : 1, first_genre :{$arrayElemAt : ["$genres", 0]}, imdb : 1, imdb_score : { $cond : [ // 특정 값이 크다면 Excel.. 2025. 6. 17.
[MongoDB] 실전 쿼리 작성 데이터 샘플{ "_id": { "$oid": "573a1399f29313caabcee864" }, "plot": "A serial adventure writer with problems in his personal life lives out the adventures of his literary hero, King of Adventurers.", "genres": [ "Action", "Adventure" ], "runtime": 91, "cast": [ "Jet Li", "Rosamund Kwan", "Charlie Yeung", "Takeshi Kaneshiro" ], "num_mflix_comments": 0, "poster": "https:/.. 2025. 6. 16.
[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.