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