Get all possible combinations from array in MongoDB aggregation 🚀
问题 How to do aggregation ( $group ) by the same values from the array? Not all at once, but few or all, if any. I can do $group by one word, but I also need all possible variations... Collection example: {"keywords": ["gta", "distribution", "keys"]} {"keywords": ["gta", "online", "moto", "races"]} {"keywords": ["gta", "online", "samp"]} Result example: "gta" - 3 matches "online" - 2 matches "gta online" - 2 matches 回答1: You could use $reduce to extract all combinations of pairs from an array.