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