As a result, the first group is returning. But there is another user in the first group. Whereas the array matches exactly with the second group. How should I write the query for exact match. Please help me.
Hello @MUSTAFA_SAHIN, welcome to the MongoDB Community forum!
If you are looking for exact match with the elements of an array you need to use $setEquals aggregate operator. To use this operator within the find method’s filter, you use it with another operator $expr. The $expr allows using aggregate operators within the find method.
I think the OP wants to get the second document as the result.
The data (short version):
id:1 has members with username: "john22", "david7", "william4"
id:2 has members with username: "john22", "david7"
I think, as per the OP’s note above, the solution is to get the exact match ( ‘john22’, ‘david7’ ) would be, to find the document with id:2only.
Well, it turns out there are quite a few operators to deal with when working with matching array data with array data (exact matches, partial matches, etc.). Finally it is a matter of the requirement, not choice.