[Lab Computing fields] The argument to $size must be an array, but was of type: missing

Hi,

I have successfully split the title into words in an array, but when I use $size I get that the argument is of type ‘missing’. Here’s a sanitized version of the code used (just trying to get a list of movie titles, the array and the array size).

But somehow it marks all size_array to “NA”, so title_array isn’t an array despite displaying correctly like this:

{ "_id" : ObjectId("573a1390f29313caabcd421c"), "title" : "A Turn of the Century Illusionist", "title_array" : [ "A", "Turn", "of", "the", "Century", "Illusionist" ], "size_array" : "NA" }
{ "_id" : ObjectId("573a1390f29313caabcd4cf1"), "title" : "Ingeborg Holm", "title_array" : [ "Ingeborg", "Holm" ], "size_array" : "NA" }
{ "_id" : ObjectId("573a1390f29313caabcd41f0"), "title" : "Ella Lola, a la Trilby", "title_array" : [ "Ella", "Lola,", "a", "la", "Trilby" ], "size_array" : "NA" }

Any ideas where am I screwing up the syntax? Thanks!

Hey @Lolo_40041

Not so much the syntax but when you create a field with $project (title_array), it is not available for use in the same $project. You will have to add another $project stage to the pipeline.

Hope that helps

1 Like

Hey @Lolo_40041

One last favour, could you remove the code from your post? It is kind of against forum guidelines to post any potential answer code. Thanks a lot :wink:

@natac13 Sure, done.

Thanks to your reply I was able to complete the Lab, I appreciate it. One last question, let’s say I want to display on the shell the results. I’m trying something like this:

db.movies.aggregate([
	{<create variable of title turned array>},
	{<check array size>},
	{<match only array size 1>},
	{$project : {_id:0,title: 1, title_array : 1, size_array: 1} } 
])

(I hope this example is not against the rules)

And the reply I get is:

{ "size_array" : 1 }
{ "size_array" : 1 }
{ "size_array" : 1 }

Which is somewhat expected, but what happened to the fields ‘title’, ‘title_array’ and ‘size_array’?

Thanks!

Nope this is perfect!

Consider what the docs have to say about $project

Passes along the documents with the requested fields to the next stage in the pipeline. The specified fields can be existing fields from the input documents or newly computed fields.

Therefore, if the below stages are $project stages then you would have to include the fields you want so they make it to the next stage.

{<create variable of title turned array>},
{<check array size>}

Take a look at $addFields which is similar to $project