Chapter 4: Advanced CRUD Operations (Lab 2: Logic Operators)

Here is the question to solve it

Problem:

To complete this exercise connect to your Atlas cluster using the in-browser IDE space at the end of this chapter.

How many companies in the sample_training.companies dataset were either founded in 2004 and either have the social category_code or web category_code, or were founded in the month of October and also either have the social category_code or web category_code?

Copy/paste the exact numeric value of the result that you get into the response field.

Summary

Here is the answer which I saw once my answer got failed

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

But I think, this is wrong query per problem. I would say, this would be the right query

Summary

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

Any suggestion on this

The problem asks to account for 2 scenarios:

founded_year = 2004
category_code = either “web” or “social”

founded_month = 10
category_code = either “web” or “social”

As written, your solution appears to be checking for the following:

founded_year = 2004
category_code = either “web” or “social”
founded_month = 10

founded_year = 2004
category_code = either “web” or “social”

I had a similar solution AARZOO_MANOOSI, and spent an hour trying to make it work to prove to my self that I understood the core concepts of the class. I then realized that I should analyze the question further before querying for a solution. Lets say that the query you listed is correct theoretically, you leave room for many errors somewhere in your query, like missing curly, square brackets, and commas. The correct query provided by the class is simpler and concise, remember that “how to simplify a complex problem” is a key concept in programming .You should look for more problems to solve and practice to get a better understanding of how to analyze questions. You can do it!

2 Likes

Hi @Hamza_Salah,

Thanks for sharing your thoughts. We are also working on creating some additional MQL practice exercises for this course to make the learning experience of our learners better.

I have written this query and seems to be working fine, without errors but not giving me correct result. Can someone please help.

db.compnaies.find({ “$or”: [{ “$and” :[{“founded_year” : 2005},{ “$or” :[ { “category_code”: “social” }, { “category_code”: “web” } ] } ]}, {"$and":[{“founded_month” : 10},{ “$or” :[ { “category_code”: “social” }, { “category_code”: “web” } ] } ]} ]}).count()

There is a typo in your collection name
Are you getting zero results or some other value than what you expected?

@Ramachandra_37567 I am getting 185 rows, apparently that is wrong. Sorry I pasted incorrect code. Attached is the image

I think founded_year should be 2004 but you are using 2005?
Please check logic of your query and usage of or & and operators
This thread has all required details above

2 Likes

Thank you @Ramachandra_37567, that helped!.

2 posts were split to a new topic: Chapter 4: Is this correct?