Ticket: User Report

I implemented:

group_stage = { “$group”: {"_id": {"_id": “$email”}, “number of comments” :{"$sum":1}} }
sort_stage = {"$sort":{“number of comments”: -1}}
limit_stage = {"$limit": 20}
pipeline = [ group_stage, sort_stage, limit_stage ]

rc = db.comments.read_concern("majority") # you may want to change this read concern!
comments = db.comments.with_options(read_concern=rc)
result = comments.aggregate(pipeline)

But which looks similar to the detailed answer provided. But this did not help me get the validation token and i could not submit the answer.

1 Like

Run the test:

pytest -m user_report

and you will see the error in your code.

1 Like

I have similar code. I built the pipeline in compass first. pytest doesn’t return any errors.

However, when I checked the results in Compass against what the test was expecting, the test email {email: ‘roger_ashton-griffiths@gameofthron.es’} is only counted 607 times in the pipeline but when it is applied as filter to the documents there are 909 results returned.

Why would the pipeline count differently than the filter?

Kunal,

Check your data type.

Not sure what you mean.
We are counting the instances of an email as a string, so it should be correct that the data type will always be a string since a comment can only have one author.

Robert,

Notice I was referring to Kunal’s question, not yours. I would need to see your pipeline code to see if there was any issue with it.

Hi Greg,

My bad, sorry.

Below is my pipeline code:

pipeline = [
{
‘$group’: {
‘_id’: ‘$email’,
‘count’: {
‘$sum’: 1
}
}
}, {
‘$sort’: {
‘count’:-1
}
}, {
‘$limit’: 20
}
]

Any help is appreciated.

No worries. There should not be a difference and I can tell you that your pipeline is correct. When I filter in compass with {email: ‘roger_ashton-griffiths@gameofthron.es’} I get 909 documents.

Greg

Weird, when I enter it into the shell I get the correct result.
So, I suppose the question is, why is it not returning correctly in Compass and pyMongo.

The UI says: ‘User Report: Invalid response to good user report request’

Greg,
I’m having this error when run user_report test:
rc = db.comments.read_concern({“w”:“mayority”}) # you may want to change this read concern!
E TypeError: ‘ReadConcern’ object is not callable

I’m reading the mongo docs and can’t find the right way to set the call to readConcern.
I’ve changed the name to ReadConcern() but still get error. That’s what I see on the import line.
Thanks for your advice.

Have the variable rc like this “ReadConcern(level=“majority”)” . Ignore the double quotes in the beginning after.
And use the variable “rc”, in the comments variable:
db.comments.with_options(read_concern=rc)…

1 Like

It worked, thanks murali

Hi, I’m exactly in same situation. same code and the same result.
Have you been able to find a solution?

Any help is appreciated.

  1. $group - _id with name and email , don’t forget $sum to “count” (or whatever you like)
  2. $sort - DESCENDINGly sort on “count”
  3. $limit - 20
  4. $project - “_id.email” as “_id” and “count”
    Hope this help.

Thanks for your message.

However I was wondering why I don’t get the same result in Compass where the count for “roger_ashton-griffiths@gameofthron.es” is 607 instead of 909. Apparently, some other students had the same issue.

3 Likes

I am having exactly the same problem as Iman_06108. Can someone please help. here is my code below:

    // TODO Ticket: User Report
      // Return the 20 users who have commented the most on MFlix.
      const pipeline = [
        {
          $group: {
            _id: '$email', 
            count: {
              $sum: 1
            }
          }
        }, {
          $sort: {
            count: -1
          }
        }, {
          $limit: 20
        }
      ]

      // TODO Ticket: User Report
      // Use a more durable Read Concern here to make sure this data is not stale.
      const readConcern = comments.readConcern('majority')

      const aggregateResult = await comments.aggregate(pipeline, {
        readConcern,
      })
1 Like

I used $sortByCount plus $limit stages. It works in Mongo shell but not in Compass,
Any idea about it?

My pipeline looks basically the same as previous examples. It works in compass properly but for some reason running the test is not really showing relevant info about what I have wrong.

C:\Users\Gustavo\Documents\M220\mflix-python>pytest -m user_report
Traceback (most recent call last):
File “c:\program files (x86)\python37-32\lib\site-packages_pytest\config.py”,
line 327, in _getconftestmodules
return self._path2confmods[path]
KeyError: local(‘C:\Users\Gustavo\Documents\M220\mflix-python\tests’)

During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File “c:\program files (x86)\python37-32\lib\site-packages_pytest\config.py”,
line 358, in _importconftest
return self._conftestpath2mod[conftestpath]
KeyError: local(‘C:\Users\Gustavo\Documents\M220\mflix-python\tests\conft
est.py’)

During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File “c:\program files (x86)\python37-32\lib\site-packages_pytest\config.py”,
line 364, in _importconftest
mod = conftestpath.pyimport()
File “c:\program files (x86)\python37-32\lib\site-packages\py_path\local.py”,
line 721, in pyimport
raise self.ImportMismatchError(modname, modfile, self)
py._path.local.LocalPath.ImportMismatchError: (‘tests.conftest’, ‘C:\Users\gum
artinez\Documents\M220\mflix-python\tests\conftest.py’, local(‘C:\Users\G
ustavo\Documents\M220\mflix-python\tests\conftest.py’))
ERROR: could not load C:\Users\Gustavo\Documents\M220\mflix-python\tests\conftes
t.py

This is my query,

group_stage = {'$group': {'_id': {'_id': '$email'}, 'comments': {'$sum': 1}}}    
sort_stage = {'$sort':{'comments': -1}}    
limit_stage = {'$limit': 20}

does anyone has an idea what could it be? I’m basically stuck in there t.t

Regards,

Please recheck this pipeline. There are two _id 's.

Let me know if after updating the pipeline does not solve the issue.

Kanika

I found the problem
I had set the read concern incorrectly.
The pipeline is correct tho