Chapter 4 Lab 2

I am trying to solve this lab. To get started I wanted to solve for the first portion, identifying the companies founded in 2004 and that have the “category_code” of either “web” or “social”. I am using this but when I enter it in the IDE or in the terminal on my computer it just generates an ellipses and ends, I don’t get any results. Anyone able to help me understand the error in my query?

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

The ellipses is the secondary prompt of the shell. The secondary prompt is printed to indicate you that the command you are entering is not terminated. The usual cause is missing parenthesis, braces or brackets. In the above, the closing parenthesis of find( is missing. Also take a look at https://docs.mongodb.com/manual/reference/operator/query/and/ because your $and has not the correct syntax.

Hello ,
i guess you are missing a parenthesis related to .find() hope i helped a bit
have a great day

1 Like

thanks! I am getting closer, but still generating ellipses. anyone able to help me see where I am going wrong here?

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

Thanks, because with

you confirm

And as for

Your brackets are all wrong. The closing bracket after

is terminating the array of $or clause. That is not what you want.

The mongo shell and new mongosh are JS based. Simplify your query using variables. When you do everything becomes clear. Otherwise it is a mess of braces, brackets and parenthesis. For a query like this problem I would:

year = { "founded_year" : 2004 }
month = { "founded_month" : 10 }
social = { "category_code" : "social" }
web = { "category_code" : "web" }
web_or_social = { "$or" : [ social , web ] }
year_and_category = { "$and" : [ year , web_or_social ] }
month_and_category = { "$and" : [ month , web_or_social ] }
query = { "$or" : [ year_and_category , month_and_category ] } 
// and query ends up being:
{ '$or': 
   [ { '$and': 
        [ { founded_year: 2004 },
          { '$or': [ { category_code: 'social' }, { category_code: 'web' } ] } ] },
     { '$and': 
        [ { founded_month: 10 },
          { '$or': [ { category_code: 'social' }, { category_code: 'web' } ] } ] } ] }
4 Likes

@shelbs, please followup on your thread. If my previous post help you fix your issue please mark it as the solution so that other knows that it works. This will help keep this forum efficient.