A have a collection Products in MongoDB:
{
"name": "My Product 01",
"description": "This is an excelent product",
"tags": [
"AA",
"BBBB",
"C",
"DDDDDDDDDDD"
]
}
How can I find the single longest occurrence if an item in the whole collection? (if I had only this object, the longest occurrence would be "DDDDDDDDDDD" with 11 characters).
2
Answers
$unwind
totags
level. Use$strLenCP
to compute the length. Use$setWindowFields
to$rank
the length.$match
rank: 1
to select the one(s) with longest length.Mongo Playground
generates: