Ticket: user report error

Hi All,
i wrote pilpeline
const pipeline = [
{
‘$group’: {
‘_id’: ‘$name’,
‘count’: {
‘$sum’: 1
}
}
}, {
‘$sort’: {
‘count’: 1
}
}, {
‘$limit’: 20
}
]

but it gives me name which dont even exists in my db…
its giving random name…

Can anyone help please…
Thanks

Please check my post on the other thread.

Kanika

Hi @Kanika,
I am able to pass 1st test case…
but not second one, I wrote correct grouping queries still its not giving me any grouped data which has count more than 1…

Try to sort with { ‘count’: -1 }.

yes,
I just tried this and with one minor change and worked…
Thanks @steevej-1495

I am having the below issue with this same ticket. I don’t know why i am getting 1 as the count instead of exact users comment count. I am able to validate the status successfully but the test is failing thou :frowning:

Hi @Nazehs,

You need to look at the order of sort and limit stages in your pipeline. Make sure you want to sort the comments first and then limit them.

Kanika

static async mostActiveCommenters() {
/**
Ticket: User Report

Build a pipeline that returns the 20 most frequent commenters on the MFlix
site. You can do this by counting the number of occurrences of a user's
email in the `comments` collection.
*/
try {
  // 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("local");

  const aggregateResult = await comments.aggregate(pipeline, {
    readConcern,
  })
  
  console.log(aggregateResult.count());
  console.log(readConcern);

  return await aggregateResult.toArray()
} catch (e) {
  console.error(`Unable to retrieve most active commenters: ${e}`)
  return { error: e }
}

}

I guess the pipeline is correct but I can’t see how this can be fixed. In compass I get 20 documents each with _id and count. Help Please!!! @kanikasingla

Hi @Alex_16041,

I agree, the pipeline is correct. Maybe you need to check the ReadConcern for the pipeline.

Kanika

DOCS

readConcern

Starting in MongoDB 3.6, the readConcern option has the following syntax: readConcern: { level: <value> }. Mongo DB docs

Hi everyone,

I am getting the exact result that @Alex_16041 got. The pipeline is good in Atlas but returns undefined in Node.

readConcern is an optional parameter, so if it is removed, I expect to get the top 20 results, but it still returning undefined.

I even changed aggregateResult from ‘aggregate’ to ‘findOne’ for something I knew would return a document and still received this test result:

Expected: 20
Received: undefined

The other tests related commentsDAO.js (create/update/delete) are still working, so I know I didn’t do something to the connection to comments collection.

Thanks for any help you can give.

When you get undefined, it usually means that your code crash because you do something wrong. Most likely if your pipeline looks like @Alex_16041`s one then may be it is a readConcern issue as mentioned by @Ezra_16731 in the post just before yours. The error message

TypeError: comments.readConcern is not a function is the clue that something wrong is being done.

And just to be clear the line

const readConcern = comments.readConcern("local");

is wrong since readConcern is not a function.

Thanks for your reply @steevej. You are right that it was something I was doing wrong and sorry for reviving this old thread due to my own error.

At some point of working with the supplied code I had removed the .toArray() portion return await aggregateResult.toArray(). This was causing it to return undefined to the test.

Now the only error I have has to do with unmatched comment count for our frequent commenter roger_ashton-griffiths returning 277 instead of 331 due to a change in the sample dataset. You already pointed this out on the thread between @Dave_Clarke and @SourabhBagrecha. Thanks for answering that question before I asked it.

Happy coding!