Lab - Computing Fields

Hi all,
does anyone find other way to resolve?
Mine is

< deleted >

Please don’t post answers to lab questions. Thank you.

@ Gianfranco_15573

Please follow the Forum guidelines and do not post answers or potential answers here. I have edited your post to remove that. If you have a question, make it without a code example. Thanks.

I’m using one $match stage and one $project stage to identify the number of words in a movie title. Here is a sample result document:

{
	"title" : "The Godfather",
	"titleArray" : [
		"The",
		"Godfather"
	],
	"wordsInTitle" : 2
}

What is the next step?

How do I limit the results returned to only those movies where wordsInTitle = 1?

This lab is about projection, so I assume we want to use that stage. However, $match seems like the more appropriate spot.

The functions I used to generate wordsInTitle include $split and $slice, so if I limit results using $match, I think I need to use $expr, but I’m stuck.

@ Charlie_34553

Yep, you’re almost there. :wink:

You want to use $project to split the title into components – which it looks like you’ve done correctly – but then you will want one more $match to get just those documents where there is only 1 element in the title. Good luck.

1 Like

Hi…I have created variable through $project as below:
db.movies.aggregate([{"$project": {
“_id”:0,
“splitArray”: {$split: ["$title"," "]},

“title”: 1,
“isArray”: { $isArray: “[$splitArray]” }
}}]);

isArray value is returning false value…
I checked this because when i applied $size operator, it results as NA .Actually splitting is correct but $isArray is not considering as array. Can anybody please resolve this issue?

@Habeeba_Naaz_34696

Well, I’m not sure what you’re trying to do here, but your $project pipeline has some problems. Try splitting your current $project into two pieces; you can’t use a calculated field in the same stage as the field itself.

Also, I would strongly recommend not using the same text for a variable as the operators (i.e. “isArray” and $isArray)-- that’s a prescription for disaster in the long run. Good luck.

Thank you for your reply…can we have multiple project stages in same aggregation?

@Habeeba_Naaz_34696

Yep. As many as you need. Of course, you can only $project fields that are in the documents at that stage – but you know that already. :wink:

Thanks for your reply…I finally solved it

@Gmunker

So this mostly seems to be a complaint, not a question. If you have a question, try re-posting this as a NEW post with your problem. Otherwise, I’m simply going to ignore this as there really isn’t anything constructive to say.

Hi!

I already used $split but I cannot use $size, because my results aren’t an array? Do I have to transform into an array or there is another way to know how many words there are in title?

If I do have to transform it into an array, how can I do that?

I don’t understand why mine doesn’t work, the documentation of $split show a similar operation as mine. With a list not an array.

It would have been much better if you created a new thread.

What chapter is this lab located?

chapter 1, Lab - Computing Fields

$split returns an array so $size should work. They both belong in separate stages.

I made both of them in $project. How should I do?


Don’t know if it h

Hint: separate stages and different types of stages.