티스토리 뷰

Database/mongoDB

[MongoDB] Aggregate FrameWork

데브포유 2017. 3. 21. 17:06
반응형

# Aggregation FrameWrok


db.employees.find().pretty();


db.employees.aggregate(

{$match : { $and: [{deptno: 10},{sal : {$gte: 500, $lte:3000}}]}},

{$match : { $or: [{job:"CLERK"},{job:"SALESMAN"}]}},

{$project :{

             _id : 0,

             empno : 1,

             ename : {$toLower :"$ename"},

             substr_nm : {$substr: ["$ename",1,2]}

             

           }

}

).pretty()


>> comm필드를 서브다큐먼트로 저장

db.employees.aggregate(

{$match : {deptno:30}},

{$project: {

_id : 0,

empno: 1,

stats: {sale : "$sal", comm:"$comm"}

}

}

).pretty()


>> comm값이 null이면 0으로 출력, $add, $subtract, $multiply, $divide

db.employees.aggregate(

{$match : {deptno:30}},

{$project: {

_id : 0,

empno: 1,

sal : 1,

comm : {$ifNull: ["$comm", 0]},

sum_avg_add : {$add :["$sal", {$ifNull:["$comm",0]}]}

}

}

).pretty()



>> group by

db.employees.aggregate(

{$group: { _id: {dept_no:"$deptno", job_nm:"$job"},

          sal_num: {$sum:"$sal"}   

         }

},

{$sort: {_id:1}}   

         

).pretty()





- EOF -



반응형