MongoDB分组聚合多重条件过滤
db.getCollection('T_D_UserCourseRecordSpecialTrain').aggregate([
{'$match':{"CourseID": "1889"}},
{ $group: { _id : '$UserCode', count: { $sum : 1 } } },
{ $match: { count: { $gt : 1} } }])
db.getCollection('test').aggregate([
{'$match':{'test_a':{$eq:0} } },
//过滤条件
{'$group':{'_id':{user_id:'$user_id',date:'$date'} } },
//多条件分组聚合
{'$group':{'_id':'$_id.user_id','count':{'$sum':NumberInt(1)} } },
//多重分组聚合
])
#Java实现Mongo多条件分组聚合:
Aggregates.group(new BasicDBObject("_id",
new BasicDBObject("user_id","$user_id").append("date", "$date"))
)
评论区