Chapter 3, Lab: $group and Accumulators

I’m really struggling with this one - I cannot seem to get the same values as any of the suggested answers.

I’m using regex in my aggregation group to group documents where the 1st word is “Won” but the values I get are:
“highest” : 9.6,
“lowest” : 4.5,
“average” : 7.4907,
“stddev” : 0.6537

Not sure how I can show my ‘workings out’ - although its obviously wrong so won’t help anyone :slight_smile:

That’s not enough to satisfy this requirement :slight_smile: :
image

This is my conditional regex match - my assumption is that if it starts with “Won” then we group it under the group “winner”, if not, it goes into the 0 group … sorry - bad explanation. I’,m grouping on a user defined field : ‘oscar’ , and I calculate 2 groups, 1 with 43235 - the ‘0’ group (! Won), and the other with 1262 documents - the ‘winner’ group.

oscar: {$cond: [ { $regexMatch: { input: “$awards”, regex: /^.*Won/ }, }, “winner”, 0 ]}

Can you explain why the above is resulting in incorrect values? Happy to show my full workings out if it helps. I’m just spinning at present.

Would your regex match these sentences

  • Wayne Won 999 Grammys
  • Won another award

Spin class and working at the same time… multitasking :slight_smile:

Argh! :slight_smile:

Ok, so understanding the data is critical … I made the assumption (dangerous) that there were only Oscars to be won. I’ve now changed the regex to : /^.Won \d Oscar/ and out pops the right answer.

I’m happy I was on the right lines, but happier that you were there to save the weekend :slight_smile:
Thanks!

There are Grammys and BAFTAs to be won too :slight_smile:

PS: The dot is not required and are you sure it’s matching “Won 13 Oscars”?

re: “.” - I wasn’t sure if there were any other weird chars before the first W, so decided (im no regex expert either) to cover my bases as best I could.

I got the right answers after making the adjustment, so I guess it must be catching all the records - no cheating - scouts honour.

I did (thoughout my many blind bumping-into-brick-wall attempts) try to change the stddev method to Pop on the off-chance, which matched 1 set of values (incorrect answer), but when I reverted back to the stddevsamp method, the values matched.

\d on it’s own is only going to match a single digit. It’s a coincidence that you’re getting the correct result if it’s not catching out double digits.

By the way, at the bottom of the lab check out the Detailed Answer to see their approach.

Happy learning!

I think the forum has interpreted the asterisk after the \d as a special char - hence why it’s not showing up in the post. If you’re able to view the raw text, it’s there. Made me double check my results though :slight_smile:

Asterisk after the \d isn’t quite ideal too. Have a look at the Detailed Answer, you’ll see what I mean. :slight_smile: