Incorrect Explanation in Lecture Query Operators - Logic of Chapter 4: Advanced CRUD Operations

Starting around 4:17, the video says

If we don’t specify an explicit $and like this and just list one $or after another, we’ll get documents that match either of the two $or queries.

From testing, I found that the user interface attempts to parse a JSON object from the text box and ignores all characters after a JSON object is successfully parsed.

This means that we actually get documents that match the first $or query, while the second is ignored. In fact, if you replace the comma and second query with random text, the query will still execute and return the same results.

Please provide examples.

and

Which

The user interface is the text box that looks

image

If we use a different example, consider the following filter:

{"filter":"example"}

and this filter

{"filter":"example"},{"filter2":"example2"}

and this filter (showing how random text after the first JSON object is ignored)

{"filter":"example"}abc

All 3 filters behave the same, as only the first JSON object is used for the query.

Which version of Compass?

This really looks as a bug. Most of the time if the query is not a single well formed JSON document, you should not be able to press the Find button. I could Find with

{"filter":"example"},{"filter2":"example2"}

the funny thing is that the query is not added to the history. In addition, the query return all documents, even if I have to no fields named filter or filter2. So I am not sure about

For me

{"filter":"example"}abc

did not work. The Find button was not enable. But it did with

{"filter":"example"},abc

This is the same thing all documents are returned but the query is not saved in Compass history.

It might be a manifestation of JavaScript comma operator as explained in

So I went to the mongosh and

q = {"filter":"example"},{"filter2":"example2"}
// produces
{ filter2: 'example2' }

The mongo shell produced the same result. That’s reassuring, because it is valid JS.

Both mongo and mongosh produced syntax error with { f:1}abc and undefined reference with {f:1},abc. Both are normal JS behaviour.

The conclusion is that JS error or exception is ignored silently in Compass but is handled correctly in mongo and mongosh. My version of Compass is 1.22.1

I would not know how to proceed from here but I think @victorz faced a real issue.

I am using the web interface at cloud.mongodb.com. Perhaps Compass behaves differently (parses the entire input filter as a JavaScript expression, disabling the Find button if it is not valid) compared to the web interface (parses one object expression, ignoring everything after it, returning an error if it is not valid).

In the web interface, I created a test collection with three documents:

{"_id":{"$oid":"61129f0dd4d9c7314a7d858d"},"filter":"example","a":"a"}
{"_id":{"$oid":"61129f2bd4d9c7314a7d858f"},"filter":"example","a":"b"}
{"_id":{"$oid":"61129f3ad4d9c7314a7d8590"},"filter":"example2","a":"c"}

If the filter query is blank, all 3 documents are returned.

If I run the query abc, the Find button is enabled, but I get this error:

The provided parameters were invalid. Check your query and try again.

image

Each of the following queries returns the first two documents without error:

{"filter":"example"}
{filter:"example"}
{"filter":"example"}abc
{"filter":"example"}!@#$%^&*()clearly invalid JS Expression!@#$%^&*()
{"filter":"example"},{"a":"c"}

This is contrary to the lecture video, which suggests that the last query would return documents that match either of the two queries: all documents that match {"filter":"example"} or {"a":"c"}.

If the comma operator is being applied in your version of Compass, it should return documents that match only the second operand of the comma operator, which is also contrary to the lecture video, as the first filter (the one before the comma) would be ignored.
Here’s a possible explanation for your observation: perhaps if the variable abc is not defined, it causes the expression {},abc to evaluate to undefined, which results in no filter being applied.