mongodb 聚合语句语法示例
db.collection名.aggreate([
{'$lookup':{
from:'待被join的collection名',
localField: '本collection的feild',
foreignField:'待被join的collection的外键名',
as:'命名查询结果展示字段'
}},
{'$match': {'$or':['age':{'$lt':18}, 'gender':'male'],'name':'xiaoming'}},
{'$group':{'_id':'$gender', 'counter':{'$sum':1}}},
{'$group':{'_id':{'gender':'$gender','age':'$age'}, 'age_avg':{'$avg':'$age'}}}}
{'$project':{'_id':0, 'name':1,'counter':1}},
{'$sort':{'counter':1}},
{'$skip':2},
{'$limit':5}
{'$unwind':'$size'},
{'$unwind':{'path':'$size', preserveNullAndEmptyArrays:true}}
{'$project':
'discount':{
'$cond':{
'if':{'$gt':['$age', 35]}, 'then':'社会人',
'else':
'$cond':{
'if':{'$and':[{'$lt':['$age', 35]},'$gt':['$age', 22]]}, 'then':'大龄青年',
'else':'大学生'
}
}
}
}
}
])