Chapter 3 $unwind Stage Using $trunc

I got the right answer, but I couldn’t get the “average” field to trunk to 1 decimal place. I used the $trunc command in the $group stage and I tried to embed it into the
“average”: {$avg: “$imdb.rating”} command but I got the error that it needs to resolve to a number not a string.

I tried to add field and trunc the imdb.rating field but again, I got an error that it needs to resolve to a integer not a string. According to the inspection of the schema, the initial value is stored as a number. What is the source of this error?
I’m not posting code cause I do not want to give the answer away.

Hello @David_Thompson, you can try applying the $trunc on the resulting average value. For example, I have a sample document with a field { n: 1234.56789 }, and I want to truncate it to a value with 2 decimals.

  { $addFields: { n: { $trunc: [ "$n", 2 ] } } }

This returns the value of n with two decimals: 1234.56

Thank… However I cannot get access to the numbers in the nested document… Do I need to pull them out with a map or unwind statement? Also, Can I apply unwind to two fields at the same time? I don’t think this is possible.

I’m trying to access {$trunc: [$imdb.rating, 1]}

Try applying the $trunc on the average field, after the average is calculated in a separate stage (as shown in my example above).

Ok… For this lab. I use the following stages

$match - To select on the cast array and languages array (I didn’t include imdb.rating since it is included in documents.

$unwind - to unwind the cast array

$addfield - to add the integer field for numfilms… I tried to move the calculation of “$ave”:{$avg: imdb.rating} here but I cannot get it in the group stage (I use the “average”: $ave but it errors out.)

$group - to group based on the output wanted (“first last”, number of films, and average rating)

lastly I used $sort to sort by numfilms -1

My question is the $avg: operator is done in the $group stage. Where in here am I supposed to use the $trunc operator? I cannot use it in the $group stage (it isn’t a valid command in group to my knowledge)

So… This question is only for my knowledge, I have already solved the problem. I am just wondering why I cannot $trunc an embedded document: It will not access imdb.rating. I get the alert imdb is undefined. This would lead me to believe that in VS Code, I would need to stipulate or declare a variable. Is this done with the $$ sign? and if so… How do I take a field in an above stage and reference it as a variable $$ in the stage where the integer is needed? It seems to error out.

I think you cannot use the $trunc in the $group stage - hence my previous suggestions.

Yes… you are correct… So… If I cannot use it in the $group stage… and this is where the average is calculated… Then where do I move it to? I can do one of two ways… I can calculate the $avg outside of the group stage (it seems that that this errors out though) or I can access the imdb.rating outside and trunc it there… (add a field in the $addFields section and trunk the field. Then use the new field in the average calculation)… This also seems to not work. If I use “ave”: {$trunc: [$imdb.rating, 1]} I get the alert “imdb not defined”… if I use “$imdb.rating” , 1 I get the alert that imdb isn’t a number…

So… I will chalk this one up to study further later and just move on… I don’t have time to hunt and peck for the answer. I will have to read more when I get the time.

Add the $addFields stage to the pipeline (from my example) after the $group stage. In the $addFields stage apply the $trunc on the calculated average field to get the value’s decimals truncated to 1. For example:

  // ... other stages
  { $group: { _id: "$someField", average: { $avg: "$someOtherField" } } },
  { $addFields: { average: { $trunc: [ "$average", 1 ] } } },
  // ... other stages

Thanks… Got it now.