Yeah This method has been tried.
According to the id grouping, if the id is designated as a combination item, because the id will not repeat, the effect is equivalent to de-duplication of the combination item.
Let me add to you that if you need to come up with other field of the collection now, you can use the $push keyword.
//group by name and sex
//put the grouped name,sex,age under the corresponding Document to form an array
db.student.aggregate(
[
{
$group:{
_id: {name: "$name", sex: "$sex"},
name: {$push: "$name"},
sex: {$push: "$sex"},
age: {$push: "$age"}
}
}
]
). forEach(function(x){
db.temp.insert(
{
name: x.name,
sex : x.sex,
age: x.age
}
);
});