Lab -- Computing Fields

   _id: 0, 
   result: { $expr: {$eq: [$size: { $split: ["$title", " "]}, 1]} }

I’m getting the following error:

SyntaxError: Unexpected token, expected “,” (1:115)

1 | db.movies.aggregate([{ $project: { _id: 0, title: 1, result:{$expr: {$eq: [{$size: {$split: ["$title", " "]}}, 1]} )
| ^


Hello @Chris_Job1 and welcome to the MongoDB community.

Basically the error is stating that your query is not properly formed. In this case it’s at character 115 of the first line, which is the last character. The command parser seems to think it wants a comma at that location.

If we go through and reformat your code so you can see how your brackets line up, you can see that you are missing several closing brackets:

            $project: {
                _id: 0,
                title: 1,
                    $expr: {
                        $eq: [
                                $size: {
                                    $split: ["$title", " "]
                            }, 1

Once you put the missing closing brackets in, you will notice that you have another error. I’ll let you work through that so that you can better learn the lessons the course you’re going through.


Thanks for the assist!! Didn’t realize I was missing so many brackets!!
Looks like I didn’t need the $expr operator

Now I’m getting:
{ title: ‘A Fantasy’, result: false },
{ title: ‘Frankenstein’, result: true },
{ title: ‘The Woman Always Pays’, result: false },
{ title: ‘Cleopatra’, result: true },

Any suggestion as to how I show only the ones that are true?

Since you’ve now got a field in the results called result that has a boolean value, you can add a $match stage at the end of the pipeline to filter on only those records.

    {"$project": {...}},
    {"$match": {...}}

Again I’ll let you write out the $match statement to help your learning and retention of things.


Thank you!!

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.