aggregation-framework

Get all possible combinations from array in MongoDB aggregation 🚀

此生再无相见时 提交于 2020-12-06 16:18:09
问题 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.

How to push multiple columns' value within group

大城市里の小女人 提交于 2020-12-05 07:23:28
问题 It seems my query will won't act as my expect I want to group by name and mapping location1 and location2 into an array called locations Data {name: "Jack", localtion1: "Taiwan", localtion2: "France"}, {name: "Jack", localtion1: "Japan", localtion2: "Taiwan"}, {name: "Mary", localtion1: "China", localtion2: "Taiwan"} Expected result { name: "Jack", localtions: ["Taiwan", "France", "Japan"] }, {name: "Mary", localtions: ["China", "Taiwan"]} Query db.collection.aggregate([ { $group: { _id: "

How to push multiple columns' value within group

此生再无相见时 提交于 2020-12-05 07:23:15
问题 It seems my query will won't act as my expect I want to group by name and mapping location1 and location2 into an array called locations Data {name: "Jack", localtion1: "Taiwan", localtion2: "France"}, {name: "Jack", localtion1: "Japan", localtion2: "Taiwan"}, {name: "Mary", localtion1: "China", localtion2: "Taiwan"} Expected result { name: "Jack", localtions: ["Taiwan", "France", "Japan"] }, {name: "Mary", localtions: ["China", "Taiwan"]} Query db.collection.aggregate([ { $group: { _id: "

Mongo DB aggregation array size greater than match [duplicate]

一曲冷凌霜 提交于 2020-11-30 12:37:30
问题 This question already has answers here : Query for documents where array size is greater than 1 (14 answers) Closed 2 years ago . I have a collection where investments is an array inside the mongodb document. Now using aggregation I am trying to filter results where investments length is more than 5 times and then do the next processing using match query. Collection{ _id:000000 --------------------- "investments" : [ { hhhhhhhhhhhhhh }, { hhhhhhhhhhhhhh } }] ----------------- The match query