In this lesson, we're going to discuss the $elemMatch operator. And specifically, we're going to discuss a common pitfall that you might be subject to when you're querying for subdocuments that are embedded in an array field. And we're going to talk about, specifically, how we can use the $elemMatch operator to eliminate this common pitfall that people fall into. Unfortunately, the influx app doesn't need to query for documents by subdocuments that are embedded in an array field. So instead, we're going to walk through kind of a contrived example because this topic $elemMatch is actually really important. So let's go ahead and take a look at a random document in our movies collection. And here, I'm making sure that it actually has a comments field. And I'm just going to go ahead and project out the comments. And just so that we're all on the same page, as you can see, comments is an array field. And each element in this comments array is actually an embedded document. And this is the kind of schema where you'll encounter this specific pitfall. Let's go ahead and go down to cell five. Now, we know from previous lessons that we can query for this document here using dot notation. Here I'm saying I want to find a single document where a comment was made by Samwell Tarly. And awesome, we can go ahead and see here that here is our documents. And there are three comments, and one of them is from Samwell Tarly. But let's look at another example. Let's say that I wanted to actually find this document, not only by one field, but actually by two fields. If we look at our past result, you can see that this comment by Samwell was actually made in 1988. So let's say I was having a query where I not only wanted to find this document by the commenter's name, but also when this specific commenter made the comment. In this case, I want to make sure that it is less than, or earlier than, January 1st, 1995. And when we run this query, we get the same document. We have the same three comments from the same three commenters. This is the exact same document as our last query. So we know that we can actually query for documents based on multiple fields when they're embedded in an array. But there's actually a pitfall in our logic here. We're actually returning this document not purely based on this criteria. If this was not a find one, we would actually return more documents than what we'd expect. So let's go ahead and look at this. If you scroll down to cell seven, here you can see that I'm running the same exact filters, projection. But now, I'm going to skip over this document that we've been looking at. And I'm just going to get one document. So we're just getting the next document that MongoDB will find. And as you can see here, we do have a comment from Samwell, so seems like it's good. But if we look at that date, you'll notice that the comment was made in 2000, which is not less than January 1st, 1995. And the pitfall here is that what this query is actually saying is it's not saying let's find comments from Sam that were also made before 1995. But what we're actually saying is let's find documents where one of the comments is from Sam, and also one of the comments is from before 1995. We're not specifically saying that Sam's comments have to be before 1995. But that's probably what the person who wrote this query was actually looking for. And this is the common pitfall. And the way that we get around that is by using the $elemMatch operator, and here's the syntax. Here we say $elemMatch, of course we say first the field that we're querying on. And then we're saying, for this field, what do we want our documents to look like? Well we want our embedded documents, we want to match them based on having this name and being before this date. And now when we run this query, we're limiting to two. So we would expect to find the first document that we've been finding every time. And then we would expect to not see this incorrect second document. We'd expect to see a document where Sam, so this is our first one, Sam before 1995, all good. And if you look at our second document, we also have a comment from Sam before 1995. So let's recap what we discussed in this lesson. So we saw that when we're querying for multiple fields in subdocuments that are embedded in arrays, sometimes our logic might not be what we think it is. And we might actually return more documents than we're expecting. And to get around this issue, to actually query for multiple fields on the same embedded document that's in an array, we're going to need use the $elemMatch operator.