Lab - Using cursor-like stages

Hi, I’m trying to do the lab called “Lab: Using Cursor-like stages” in chapter 2. I managed to see num_favs, but the 25th movie is “Tar Creek” which isn’t and option. I think the problem is that it only shows me the results with num_favs = 1 because I looked every option and the movie gravity wasn’t on the list. This lab is due tomorrow so any help will be appreciated because I don’t know what I’m doing wrong.
Below I leave what I introduced on the mongo shell:

db.movies.aggregate([REMOVED]).pretty()

Hi Sara,

while you are using the $size operator, you are actually using the wrong field.

Additionally you should split your second new field of the $addFields operator in its own $addFields stage to be able to access your first one correctly.

Also, don’t forget that the “favorites” field doesn’t exist in the database… I hope this helps.

You should remove your snipped as it is a potential answer.

Greetings

First of all, thanks for your reply. I followed your advice and instead of using favorites I introduced the array with the names and used the $setIntersection command to compare it to the cast of the movies, I called this intersection favs. Then I added another $addFields stage as you sugested. This new field is num_favs: {"$size": “$favs”}
I think this should count the number of elements in the favs array field, but I get the next error. What am I missing?
Error:
2019-04-16T08:46:24.152+0200 E QUERY [js] Error: command failed: {
“operationTime” : Timestamp(1555397178, 1),
“ok” : 0,
“errmsg” : “The argument to $size must be an array, but was of type: null”,
“code” : 17124,
“codeName” : “Location17124”,
“$clusterTime” : {
“clusterTime” : Timestamp(1555397178, 1),
“signature” : {
“hash” : BinData(0,“vdl4SPspHf6nss7HUBomYCBJQRg=”),
“keyId” : NumberLong(“6627672121604571137”)
}
}
} : aggregate failed :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
doassert@src/mongo/shell/assert.js:18:14
_assertCommandWorked@src/mongo/shell/assert.js:536:17
assert.commandWorked@src/mongo/shell/assert.js:620:16
DB.prototype._runAggregate@src/mongo/shell/db.js:260:9
DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1062:12
@(shell):1:1

Hi,

if your first stage is called “intersection_favs”, where does the {"$size": “$favs”} in the second stage come from? You want to calculate the size of the documents returned by $setIntersection, don’t you?

Greetings

Maybe I didn’t explain it property the name is not “intersection favs”, I wanted to say that the name of the intersection is “favs”. Sorry for the misunderstanding.

Ah, I see. You should check the manual of $setIntersection again. It will return 0, if a field doesn’t exist. You should cover this case in your match stage.

I can’t make it work, whenever I change something there is always another error. Anyway I managed to do the lab using Compass.
Thanks for your replies. :slightly_smiling_face:

In the first stage of pipeline, you must make $match by checking only the fields that are arrays

1 Like