Chapter 1: Optional Lab (includes possible answers)

So i try to do it this way thinking it would work. I assigned the new project field in the first project and then matched at the end. This way it returned 1259 results. But if I did the second projection it returned the 1596. Why does that happen?

<deleted>

a computed field is not available in the stage it is created. in your case writers as the computed field with the split is not available in commonToAll setIntersection.

1 Like

Oh that it was I thought it could be happen. The weird thing is that commonToAll worked great, so I thought writers were filling up that array (as it find the common thing in the 3 fields)

1 Like

I was also about to ask the same. @Gabriel_Martinez, really like your solution for finding non-empty and non-null arrays at the end in the $match part.
Thanks @steevej-1495

1 Like

Hi @Gabriel_Martinez,

I have edited your post to remove the aggregation pipeline.

@Manish_Chumun, @Gabriel_Martinez If you still have any questions about the aggregation pipeline, feel free to reach out and we can discuss in detail.

Thanks,
Sonali

I am having difficulty with this one, too. I used the pipeline generator in Compass, but although Compass accepted it, it still showed the count as 44488. This tells me that it is not right, because that is not one of the choices.

According to The documentation on $setIntersection, it should be correct to put all three arrays in the same $setIntersection line. However, I notice in my preview documents that it was not setting an intersection: it included documents that did not meet the criteria. You can see a screen shot here:

Yes you can. Example:

mongo > db.test.drop()
true
mongo > db.test.insertOne( { a : [1,2,3,4] , b : [2,3,4,5] , c : [3,4,5,6] } )
{
	"acknowledged" : true,
	"insertedId" : ObjectId("5f84cc84ec3d7d16ec63f083")
}
mongo > setIntersection = { $setIntersection : [ "$a" , "$b" , "$c" ] }
{ "$setIntersection" : [ "$a", "$b", "$c" ] }
mongo > db.test.aggregate( { $project : { result : setIntersection } } ).pretty()
{ "_id" : ObjectId("5f84cc84ec3d7d16ec63f083"), "result" : [ 3, 4 ] }

There are notes about the writers array in the lab description may be it is related to your issue.

The notes you refer to include commands that throw errors even when I use the copy paste:

…which means that they are unhelpful.

So close - I am using $setIntersection on the three arrays (already cleaned up writers in a prior stage)
$setIntersection: [ “$cast”, “$writers”, “$directors”]
But the results I am getting look like an array of arrays.

For cast, writers or directors my output looks like

“cast” : [
“name1”,
“name2”
]

but the output from my $setIntersection looks like:

“output”: [
[
“name1”
]
]

and if there were no names in common it looks like:

“output”: [

]

Everything I read indicates that $setIntersection should return an array, but don’t understand why I am getting an array of arrays. So any method I use to try to determine if it is empty doesn’t seem to work as it appears that an array that contains an empty array is not empty.

The result should not be an array of array. See the following example:

mongo shell > db.setIntersectionTest.find()
{ "_id" : ObjectId("5f84cb0cec3d7d16ec63f082"), "a" : [ 1, 2, 3, 4 ], "b" : [ 2, 3, 4, 5 ], "c" : [ 3, 4, 5, 6 ] }
mongo shell > db.setIntersectionTest.aggregate( { $project : { result : { $setIntersection : [ "$a" , "$b" , "$c" ] } } } )
{ "_id" : ObjectId("5f84cb0cec3d7d16ec63f082"), "result" : [ 3, 4 ] }
mongo shell > 

It must be something you do in a subsequent stage. Are you doing any $push, $unwind?

That is my understanding as well. Every example I have seen of $setIntersection returns an array.

I played around a lot. I actually had a response all typed up with “I’ve tried this and this and this and this… but it still doesn’t work”

Right before clicking reply I spotted something.

I had:
“multiFolk”: [ {$setIntersection: [ “$cast”, “$writers”, “$directors”] } ]

It is the extra set of square brackets that caused the problem.

Changed it to:
“multiFolk”: {$setIntersection: [ “$cast”, “$writers”, “$directors”] }

and it worked.

1 Like