需求说明:
统计student 表中,name和birthDate相同的学生出现的重复记录。
方案:
res = db.student.aggregate( [
{ $group: { _id: {"$name", “$birthDate”},
count: { $sum: 1 } } },
{ $match: { count: { $gt: 1 } } }
] )
for(var i=0; i<res.result.length; i++) db.test.insert(res.result[i])
关键点:
having的实现。
参考资料:
http://docs.mongodb.org/manual/reference/sql-aggregation-comparison/