如何返回MongoDB中存在的数组中的重复元素(忽略大小写)?
输入(MongoDB中的文档)
{
"userID" : "USER001",
"userName" : "manish",
"collegeIDs" : [
"COL_HARY",
"COL_MARY",
"COL_JOHNS",
"COL_CAS",
"COL_JAMES",
"col_mary",
"COL_JOHNS",
"COL_JOHNS"
]
}
预期输出:
{ "collegeIDs" : ["COL_MARY", "col_mary", "COL_JOHNS"] }
注意:
我尝试过的事情
db.myList.aggregate([
{"$project": {"collegeIDs":1}},
{"$unwind":"$collegeIDs"},
{"$project": {"collegeIDs": {"$toLower": "$collegeIDs"}}},
{"$group": {"_id":{"_id":"$_id", "cid":"$collegeIDs"}, "count":{"$sum":1}}},
{"$match": {"count":{"$gt":1}}},
{"$group": {"_id": "$_id._id", "collegeIDs": {"$addToSet":"$_id.cid"}}}
])
答案 0 :(得分:1)
以下查询可以为您提供预期的输出:
db.check.aggregate([
{
$unwind:"$collegeIDs"
},
{
$addFields:{
"collegeIdToLower":{
$toLower:"$collegeIDs"
}
}
},
{
$group:{
"_id":"$collegeIdToLower",
"collegeIDs":{
$addToSet:"$collegeIDs"
},
"count":{
$sum:1
}
}
},
{
$match:{
"count":{
$gt:1
}
}
},
{
$unwind:"$collegeIDs"
},
{
$group:{
"_id":"NO_ID",
"collegeIDs":{
$push: "$collegeIDs"
}
}
},
{
$project:{
"_id":0
}
}
]).pretty()
输出:
{
"collegeIDs" : [
"col_mary",
"COL_MARY",
"COL_JOHNS"
]
}
我们将添加一个字段用于分组,而不是将实际的大学ID转换为较低的字符串。