M001 - Chapter 4 - Need of "$and" operator

Hello, I’m Luis, a MongoDB Student.

In the Course “M001 : MongoDB Basics”, I think there are a couples of errors about the supposed implicit operator “$and”.

One of them I explain it here: Similar Queries producing different result, help wanted

The other error, I saw in the video of the chapter 4 “Lecture: Array Operators and Projection”, that it show us
if we want to know all documents that have one element of an array with the field “score” greater than 85, we have to use this query:

db.grades.find({ “class_id”: 431 },{ “scores”: { “$elemMatch”: { “score”: { “$gt”: 85 } } }}).pretty()

And also says: “The results shows all the documents that match the query, but we’re not getting every field value for every document.
For some documents, we’re not getting anything other than the _id added value, since that is the default projection behaviour.”

This is correct. Although, with this query we won’t have the correct answer. This is because without the implicit “$and” , the second clause ( { “scores”: { “$elemMatch”: { “score”: { “$gt”: 85 } } } ) it’s a projection,
so we will have all documents with { “class_id”: 431 }, and only will “show pretty” the documents thah match the query. In other words, the results shows all the documents that match the query, but the query is not correct.

Here some tests:

The query of the class:

db.grades.find({ “class_id”: 431 },{ “scores”: { “$elemMatch”: { “score”: { “$gt”: 85 } } }}).count() // 182

The correct query:

db.grades.find({"$and": [{ “class_id”: 431 },{ “scores”: { “$elemMatch”: { “score”: { “$gt”: 85 } } }}]}).count() // 87 -> The correct result.

I think this is important to say it, maybe with that, you earn a time not trying to explain these incorrect results.

P.D. Excuse my English.


Hi @Luis_Rapestre,

Thanks for highlighting. We will surely take a look and necessary action.
We wish you Happy Learning :slightly_smiling_face:


1 Like