Chapter 4 uncaught exception: SyntaxError: missing : after property id

Could you please give me a hint as to what I’m doing wrong?

db.companies.find({ "$or": 
[{"$and": [{"founded_year": 2004 }, {"category_code": "web"}, {"category_code": "social"}]}],
[{"$and": [{"founded_month": 10},  {"category_code": "web"}, {"category_code": "social"}]}]
}).count()

I just get the following message “uncaught exception: SyntaxError: missing : after property id :
@(shell):4:0”

How can I find out what is the proper syntax? Also, what is @(shell):4.0 referring to? Thanks!

Hi @redmango, the error being thrown is stating that there is an error on line 4 at position 0 (the first character). This is due to a syntax error and the shell thinks you’re missing a : at that location.

If we go through and reformat your code to more easily see the blocks, we have the following:

db.companies.find(
    {
        "$or":
            [
                {
                    "$and": [{"founded_year": 2004}, {"category_code": "web"}, {"category_code": "social"}]
                }
            ],
            [
                {
                    "$and": [{"founded_month": 10}, {"category_code": "web"}, {"category_code": "social"}]
                }
            ]
    }
).count()

This is not proper syntax as your $or block appears to have two arrays. What you want to do is put both your subdocuments for the the $and blocks into a single array as follows:

db.companies.find(
    {
        "$or":
            [
                {
                    "$and": [{"founded_year": 2004}, {"category_code": "web"}, {"category_code": "social"}]
                },
                {
                    "$and": [{"founded_month": 10}, {"category_code": "web"}, {"category_code": "social"}]
                }
            ]
    }
).count()

Give that a try and the error should go away.

Best of luck on your course and reach out to us with any more questions you have as you continue to learn.

3 Likes

Hi @Doug_Duncan and thanks for your reply. It makes it so much better to see when it’s reformatted as you did. I appreciate your tip about how to decipher the Syntax Errors too! I guess now I just have to rework my query as I got an answer of zero, which is not correct.