Lab - $group and Accumulators - Some help please

I have built my pipeline by:

  1. projecting($project) “imdb”, “awards.text” and “oscarWinner” (oscarWinner is a boolean returned by $regexMatch);
  2. matching($match) oscarWinner: true;
  3. grouping($group) by _id: null, {$stdDevSamp: “$imdb.rating”}, {$avg: “$imdb.rating”}, {$max: “$imdb.rating”}, {$min: “$imdb.rating”}

When I run this pipeline against the db.movies collection I get no result. However, if I run the same pipeline in the compass I get a result but it doesn’t match any of the options in the Lab.

I just would like to add that I managed to get a result. What happened is that I should’ve used “awards” instead of “awards.text” (the problem is that in compass awards holds a document and in the shell it holds a string). So I changed the $project stage accordingly. However, the result still not matching with the options in the Lab. I’ll keep trying to figure that out.

The above is very unlikely. Fields do not change data types based on the application used to look at them. Is that possible that you were looking at a different database or collection?

Hi @steevej! Thank you for replying!
You’re right. In Compass, I was looking at sample_mflex.movies and in the Shell, I was looking at aggregations.movies. I guess they have a different data structure.
As I mentioned before, I managed to get a result in the Shell, however, it doesn’t match with any of the options in the Lab.
Can you help me to figure that out? Thank you in advance!

This is my pipeline:

MongoDB Enterprise Cluster0-shard-0:PRIMARY> p = [
...   {
...     $project: {
...       _id: false,
...       imdb: true,
...       awards: true,
...       oscarWinner: { $regexMatch: { input: "$awards", regex: /Won/ } }
...     }
...   },
...   {
...     $match: { oscarWinner: true }
...   },
...   <deleted>

Your regex /Won/ will match the following document:

{ _id : ObjectId('573a1397f29313caabce7d5d') }

If you take a look at it you will see that what it won is not an Oscar.

1 Like

Hi @steevej! Thank you!

I changed the regex. Now it looks like this:

oscarWinner: {
        $regexMatch: {
          input: "$awards",
          regex: /^Won\s\d\sosca(r|rs)/,
          options: "im"
        }
      } 

I ran this pipeline to check the result of “oscarWinner”:

pipeLabTest = [
  {
    $project: {
      _id: false,
      imdb: true,
      awards: true,
      oscarWinner: {
        $regexMatch: {
          input: "$awards",
          regex: /^Won\s\d\sosca(r|rs)/,
          options: "im"
        }
      }
    }
  },
  {
    $match: { oscarWinner: true }
  },
  {
    $project: { awards: true }
  }
]

Now “oscarWinner” is correct, however, I’m still not getting the right answer. I should’ve gotten the right answer since “oscarWinner” holds all documents with at least 1 oscar.
Is there anything else you would point out with this new pattern?

Thank you!

Hey @steevej, I got it! Thank you so much for your help!

It was missing a \d+

Hi @Wicleice_Fabio,

I am glad you were able to debug and resolve the issue :clap:

I will be removing certain code snippets from some of the posts in this thread as it might give potential solutions to other users in the forum.

Please feel free to reach out if you have any additional questions.

Kind Regards,
Sonali Mamgain