0

I am trying to count the number of specific key-value pairs per UserID and then $bucket these results multiple times by specific conditions such as the country being "US"

I have already tried bucketing by using $match on "country" : "US", "OS" : "iOS" but I only get blank results when trying this and if I remove the $match stage then bucket works but for every single document.

{
    iOSUS: [
        {    
            "$match" : {
                    "_id": {
                        "country": "$US"
                           }
                        }
            },{   $bucket : {
                    groupBy: "$DownloadCount",
                    boundaries: [ 0, 1, 10, 20, 30, 40, 50, 100, 150, 200, 300, 400, 500],
                    default: "500+",
                    output: { count: { $sum: 1 } }
                }}
            ],


    // facetName2: [ /* add stages */ ],
    // add more facets
}

This is an example of the output I am getting for each document just before this stage. How would I place the "count" inside the "_id" so then the $match function inside $facet would properly separate the buckets?

{ 
    "_id" : {
        "UserID" : "106577122", 
        "country" : "MX", 
        "OS" : "iOS"
    }, 
    "count" : 1289.0
}
Frank Valdez
  • 77
  • 1
  • 1
  • 9
  • `"country": "$US"` - you don't need that dollar-sign there. No wonder you weren't getting any matches. – Sergio Tulentsev Jul 17 '19 at 18:46
  • @SergioTulentsev still no matches when doing that as well – Frank Valdez Jul 17 '19 at 19:05
  • Yes, that is to be expected too. Try with [$elemMatch](https://stackoverflow.com/questions/25528217/how-to-use-elemmatch-on-aggregates-projection) – Sergio Tulentsev Jul 17 '19 at 19:07
  • @SergioTulentsev I am using Studio 3T and it always says "errmsg" : "Unrecognized pipeline stage name: '$elemMatch'" whenever I try to use that. – Frank Valdez Jul 17 '19 at 19:11
  • You are supposed to use it within a `$match`, not on its own. – Sergio Tulentsev Jul 17 '19 at 19:16
  • Seems like $elemMatch doesn't work in MongoDB 3.2 or newer so $filter is the next best thing? – Frank Valdez Jul 17 '19 at 19:23
  • @SergioTulentsev All I want to know is if it's possible that in my initial $facet, could I filter out or only bucket certain documents using something like $match or $filter? Its the only problem I am currently having and haven't been able to get around it for days now. – Frank Valdez Jul 17 '19 at 19:31
  • Yes, it should be possible, one way or another. AFK now, can't give you a working solution. – Sergio Tulentsev Jul 17 '19 at 19:52
  • @SergioTulentsev That's totally fine! if you can help provide a solution at some point it would be extremely helpful – Frank Valdez Jul 17 '19 at 20:14
  • If you setup your data at https://mongoplayground.net/, it'd be easier. – Sergio Tulentsev Jul 17 '19 at 21:42
  • @SergioTulentsev I am unsure why the query won't run on the website but that is the exact query I have built it up to and then as for the configuration, I removed a lot of the fields because its mostly clutter but the reason for the $group stage is to remove that clutter but typically each document has 27 fields and not the 4 you see there https://mongoplayground.net/p/Lo9apAvQoow – Frank Valdez Jul 18 '19 at 15:32

0 Answers0