Syntax issue - or something bigger?

I’m attempting to complete the “Using Cursor-like Stages” lab, and I can conceive the solution, but I’m having what I think is a syntactic issue. I’ve spent a while trying to track it down now - so I’m not sure what else is left than publishing one of my solution steps. I’m going to attempt to fragment this post in a way that it doesn’t lead to a clear-path answer for the entirety of the problem.

  1. var favorites = included array
  2. My pipeline stages, in order, will include $match, $project, $sort, $skip.
  3. Each stage will be set as a JS variable in the Mongo shell, such that when I construct the pipeline, it will look like: var pipeline = [match, project, sort, skip]
  4. The syntax for the match, sort, and skip stages are all fine.
  5. I’m getting a Syntax Error “missing : after property id” on my project step.
  6. The intended objects exiting the project step will look like such: {“num_favs”, “tomatoes”, “title”}.
  7. Without further ado, here is my “project” declaration & assignment

var project = { $project: { "_id": 0, "tomatoes": 1, "title": 1, "num_favs": { $size: { $filter: { "cast", "isFavorite", favorites } } } } }

Here is a screen-shot of the above reformatted:

Edit: Looking at another issue, I see now that I have to handle a specific case. I’m still befuddled by my syntax problem though.

I’m guessing you’ve been able to work it out from the other discussion we just had?

No, not yet. I shelved it for a bit to think about and am on to the other lab.

I’m sure it won’t gather dust :wink: When you get back to it, I’d suggest you have a look at the $filter documentation to see some examples and you’ll notice the syntax error. In any case, $filter is probably not the ideal operator for this problem.

1 Like

Yeah I need to finish it in two days max, so I hope it doesn’t. And thanks for the tip - I saw the set operator used in the other problem - and agree that that provides a much more elegant solution.

Thanks for the help, jb. I ended up using $ifNull to solve the issue mentioned in the other thread, and so didn’t end up filtering out documents without the appropriate field.

Still not sure what my syntax error was before, but I’m much happier with the solution I came up with this time anyway.

Good to hear!

Not sure if you had a look at the documentation for $filter as suggested, you would have come across this specific example. I’ve highlighted the parts that caused the syntax error:
image

  • the labels are not optional
  • quotes are essential
  • input was expecting an array field reference, you passed a string
  • cond was expecting an expression, maybe favorites was an expression? Even if it was, the reference to the variable created in as must use two dollar signs ($$) in order to reference it. That’s how variables created within an expression are referenced.