Chapter 2 - first lab

I have gotten 95% of the query written, but keep coming up on an error to the $size command to count how many favorite actors there are in the movie.

var favorites = [
“Sandra Bullock”,
“Tom Hanks”,
“Julia Roberts”,
“Kevin Spacey”,
“George Clooney”]

db.movies.aggregate([ REMOVED ])

2018-11-19T19:16:47.440-0600 E QUERY [js] Error: command failed: {
“operationTime” : Timestamp(1542676604, 1),
“ok” : 0,
“errmsg” : “The argument to $size must be an array, but was of type: null”,
“code” : 17124,
“codeName” : “Location17124”,
“$clusterTime” : {
“clusterTime” : Timestamp(1542676604, 1),
“signature” : {
“hash” : BinData(0,“xX3+m6kFNFB8/LgPovfFzV/Ot98=”),
“keyId” : NumberLong(“6595588990781358081”)
}
}
} : aggregate failed :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
doassert@src/mongo/shell/assert.js:18:14
_assertCommandWorked@src/mongo/shell/assert.js:534:17
assert.commandWorked@src/mongo/shell/assert.js:618:16
DB.prototype._runAggregate@src/mongo/shell/db.js:260:9
DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1062:12
@(shell):1:1

First ‘favorites’ is a variable outside the pipeline.

Second when you write ‘$favorites’ inside the pipeline so you tell the aggregation framework to do a $setIntersection with the document’s fields ‘$cast’ and ‘$favorites’ but what you want is a $setIntersection with the document’s field ’cast' and the variable favorites (so no quotes, no for favorites’.

I think the null for ‘$favcast’ comes from the fact that ‘$favorites’ is null because there is no such field in the document, but you have a variable called favorites.

I hope I am ‘$helpfull’ enough without being too helpfull.

2 Likes

That totally did the trick. I need to pay more attention to how nested the variable is. Thanks.

Hi
Why the correct answer could be 26th, rather than 25th what is asked ?

I don’t understand your comment. The lab asks to write a pipeline to get the 25th film. Why do you want to be the 26th. Yes they could have ask for 26th but they want 25th. The answer they want might not make sense in the real world, they simply want to know if you can write a pipeline that corresponds to the specification.

my question is whether I have some “mistake” in query, or the answer in in the line 26 (skip = 25).
I know the correct movie, but it is 26th.

If you get the correct title by giving the title of the 26th movie then your pipeline is not correct.

How do you know the correct movie?

it is multiple choice question ! and one of the 4 answers is on the line 26 !

Did you sort for all fields in lab description?
There is the list with first 24 movies from my solution:

  • “Gravity”
  • “A Time to Kill”
  • “Extremely Loud & Incredibly Close”
  • “Charlie Wilson’s War”
  • “The Men Who Stare at Goats”
  • “The Perfect Storm”
  • “A Hologram for the King”
  • “The War”
  • “Our Brand Is Crisis”
  • “Elvis & Nixon”
  • “Toy Story 3”
  • “Envelope”
  • “Captain Phillips”
  • “Toy Story of Terror”
  • “Toy Story Toons: Hawaiian Vacation”
  • “The Green Mile”
  • “The Blind Side”
  • “Forrest Gump”
  • “Saving Private Ryan”
  • “Saving Mr. Banks”
  • “From the Earth to the Moon”
  • “Toy Story Toons: Partysaurus Rex”
  • “Shooting War”
  • “Fantastic Mr. Fox”

never mind,
the issue was in
“countries”: {$eq:[“USA”]} vs
“countries”: [“USA”],

and I also noticed that answers explained are available after submission.

Remember to project the properties that need to be sorted!

It kinda sounds obvious now. I was sure that my solution was working, but the 25th document was not one of the answers. I then added “tomatoes.viewer.rating” to the projection (which I just used for the match stage), the sorting was fixed and the answer was there.

If you feel that you’ve found the solution, but things still don’t look right, pay attention to the projection.