How do I return duplicate elements in an array (ignoring case) present in MongoDB?
Input (document in MongoDB)
{
"userID" : "USER001",
"userName" : "manish",
"collegeIDs" : [
"COL_HARY",
"COL_MARY",
"COL_JOHNS",
"COL_CAS",
"COL_JAMES",
"col_mary",
"COL_JOHNS",
"COL_JOHNS"
]
}
Expected Output:
{ "collegeIDs" : ["COL_MARY", "col_mary", "COL_JOHNS"] }
Note:
- It should only check the content and not the case.
- Return all the possibilities in the list
What I have tried
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"}}}
])