M001, Chapter 4, Lecture 2 -- subtle error!

In “M001, Chapter 4, Lecture: Query Operators - Logic” I have found a subtle error which was quite annoying for me, that’s why I share it here.

I emphasize that M001 is really a great course and all my gratitude to Yulia :), this is just a point which I think worth to mention.

At 4:15 Yulia says that “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.”

{$or: [{src_airport: "KZN"}, {dst_airport: "KZN"}]}, {$or: [{airplane: "CR2"}, {airplane: "A81"}]}

This contains the error. In Atlas FILTER button gets red, so it is clearly erroneous.

The problem is the following.

We can put an $and query explicitly like {$and: [{X}, {Y}]} and implicitly like {X, Y} where X and Y are simple field: value pairs.

But what Yulia does is {X}, {Y}. Notice the difference in using the braces. Two expressions separated with a comma instead of one expression containing two conditions.

The correct impicit {X, Y}-style query would be:

{$or: [{src_airport: "KZN"}, {dst_airport: "KZN"}], $or: [{airplane: "CR2"}, {airplane: "A81"}]}

Notice that this is one expression. The only difference from the flawed expression is the absence of two braces in the middle.

Yulia takes this wrong path of thinking further at 4:50: “So let’s add an explicit $and in the beginning. I add the operator and the include the two $or conditions in square brackets.” And she doesn’t bother the braces in any way! Yes, what she uses can be the inner part of an $and's list but not a filter expression on its own.

Anyway, understandig what the problem is, improved my mongoDB skills. :slight_smile:

2 Likes

@Rick_Qin Maybe this post will interest you. :slight_smile: It is strongly connected to yours and adds some info to @steevej 's thoughts.

Hi @sassbalint,

The above query document is invalid because it is of the form:

{
  $or: [<-Logic1->], 
  $or: [<-Logic2->]
}

This implies there are two duplicate keys($or) at the same level in the query document, which in return will only consider the “Logic-2” part, and will completely ignore the Logic-1 part of the query. Read more about this in a similar discussion that took place around 11 months ago.

Implicit $and is useful in cases like this:

//Example using explicit $and
{
  $and: [
    {x: 20},
    {y: 30}
  ]
}

So that it can be converted into an implicit $and query like this:

// Example using implicit $and
{
  x: 20,
  y: 30
}

The above was possible because the keys were different.

I hope this helps.

If you have any doubts, please feel free to reach out to us.

Thanks and Regards.
Sourabh Bagrecha,
MongoDB

1 Like

Hi @SourabhBagrecha thanks for your reply.

(Yes, you are right that my query document you cite is invalid. Sorry about that.)

But the main point of my post is that Yulia’s first query is erroneous having the form {X}, {Y}:

{$or: [{src_airport: "KZN"}, {dst_airport: "KZN"}]}, {$or: [{airplane: "CR2"}, {airplane: "A81"}]}

In other words: you cannot “just list one $or after another” as Yulia says.

The fact that there is a mistake is supported by the Atlas FILTER button getting red and resisting running this query. I think that this is truly an error and I just wanted to ask you to correct it. :slight_smile:

Hi @sassbalint,
Understood, thank you for pointing this out.
The query should probably look like this:

{
  $and: [
    {$or: [{src_airport: "KZN"}, {dst_airport: "KZN"}]}, 
    {$or: [{airplane: "CR2"}, {airplane: "A81"}]}
  ]
}

Right? Let me know if I am missing something here.

If you have any doubts, please feel free to reach out to us.

Thanks and Regards.
Sourabh Bagrecha,
MongoDB

:slight_smile: Yes the final query should look like what you wrote.

But please listen to the video from 3:50 to 5:15 and adjust the path of thinking as well. Thanks!

1 Like