Sql server has the following statement:
//Find Students with Unique Names and Sex
select distinct name,sex from student
//If converted to MongoDB, how do I de-weight this combination? MongoDB seems to only support de-weighting of a single field
db.student.distinct("name"); //Only one field is supported
db.student.distinct("name","sex"); //Wrong, does not support multiple field combination deduplication
Has the Great Spirit ever solved similar problems and asked for guidance? .................
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
}
);
});