Array Fields, finding documents matching two or more elements of array

In this particular lesson,
https://university.mongodb.com/mercury/M001/2019_July/chapter/Chapter_2_The_MongoDB_Query_Language_Atlas/lesson/595aae2236942e83e9a361cb/lecture
to find arrays containing the specified value and not an exact replica of the array specified, we use,

db.collection_name.find({“key” : “array_element”})

But there is no mention as to how to do this for two elements, for example, if I want to find the document that has William Shatner and Leonard Nimoy as cast members, and not find only those documents that have just the two of them in that particular order.

Is there any way to do so?

Hey @Aditya_Dobriyal_85801

WRONG
I think you are asking about the $in query operator, Check out the docs
Best of luck with the rest of the course. :slight_smile:

Hi @Aditya_Dobriyal_85801,

So you want to find all those documents which contain both the William Shatner and Leonard Nimoy as cast members. For this you can use $and operator.

Here in the query mentioned below we are combining two conditions together using the $and operator.

{$and:[{cast:“Leonard Nimoy”},{cast:“William Shatner”}]}

This will return all documents which has both the names in the cast field along with any other name as well.

If you still have any other query, feel free to get back to us. Happy Learning :slight_smile:

Thanks,
Shubham Rajan
Curriculum Support Engineer

1 Like

Hi @natac13,

When you are using $in operator on any query then it will return all those documents which contain even one value present in the $in field. In this case, $in operator is similar to using the $or operator.

If you have any query, feel free to get back to us.

Thanks,
Shubham Rajan
Curriculum Support Engineer

1 Like

@Shubham_13709 thanks for correcting my error. Ill have to be more carefully.

Hi @natac13,

It is such a pleasure to have a learner like you. We appreciate your engagement with the community. Keep up the good work :slight_smile:

Thanks,
Shubham Rajan
Curriculum Support Engineer

1 Like

Thanks a ton. That solved the doubt that I had.

That’s alright @natac13, I looked into the $in operator as well and that would be good for cases where I would need to return all documents where the field has any of the array elements. So it still did add it to my notes, but for this question I needed exactly the documents that have all of the elements that I mention, regardless of order. Thanks for your contribution. :slight_smile:

Hello, I wanted to know if you can extend the expiration date of Chapter 2 since just 2 days ago I was able to connect with Mongo Shell and I need 24 more hours to complete Chapter 2. Thank you