M001: MongoDB Basics, Chapter 4, Lab 2 - Why are these queries not equal?

Hi!

For this Lab I came up with a query I thought would produce the correct answer (albeit inefficiently compared to the official lab query answer), but I find it is off by 5 when compared to the official lab query answer.
I’ve been looking at this for 10mins and still can’t logically see why these queries would produce different results in the lab database. Is my logic flawed or is the query compiler doing something unexpected?
I realize my query is inefficient, but not knowing why it is off by 5 is driving me nuts. :wink: Any ideas?

Thanks!!! -michael

My query:

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()

Official lab query:

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

Hi @MichaelB and welcome to the MongoDB Community forums. :wave:

If we take a look at the documents that are returned by each of these queries, we notice that you query only returns those that have a category_code of web. This seems to be incorrect because you wanted documents that also contained a category_code of social. If you reverse the order of the values in your query, you will see that you only get five results back, and those are all category_code of social.

The reason for this is sometimes hard to spot. You have the following $or block:

Where as the answer has the following block:

If you notice, you have only a single document with the category_code field specified twice in that document. JSON does not allow for duplicate field names and will only take the last one defined when duplicates exist. This explains why all of the social documents were dropped from your results. As you can see in the answer query provided by the lab, the two values are separate documents which allows for the proper checking of two different values.

Hopefully that explains things for you.

2 Likes

(Sorry for all the deleted replies. For some reason, I couldn’t figure out how to edit my posts.)

Hi Doug and Michael,

I’m also having an issue with this problem. I’ve been running into the syntax error
Unexpected character ‘“’,
and I don’t understand why.

It seems to me that my code is identical to Michael’s except I’ve made the change recommended by Doug in his comment. I’ve been looking at this for an hour and I can’t understand why Michael’s code runs but mine fails with a syntax error.

Any help would be much appreciated.

Michael’s query:

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()

Mine:

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()

Hello @Douglas_Carter and welcome to the MongoDB Community forums.

New users are not allowed to immediately edit their posts for some reason. After some small period of activity you will be granted that ability.

I sent you a DM with the reason, but will post here as well in case others have the same issue.

If you look at the formatting of the two queries in your post, you will notice that the strings in Michael’s query are red and yours are white. This is due to you query using fancy quotes (you most likely copy/pasted an unformatted query from the forums, or as you mention in your DM an application on your system).

2 Likes

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