Help for a query finding a group whose members match exactly with a given string

hi everyone,
First of all, my English is not good, I apologize if I am rude.
I need your help with a query. I tried many things but failed.

I’ve written a little database example below.
I want to find the group whose members match exactly with the given username array.

groups.findOne( { 'members.username': { $in: [ 'john22', 'david7' ] } } )

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.

Thank you

{ groups: [

  {

    id:1,

    name:"aliens",

    members:[

      {

        username:"john22",

        joineddate:222222222,

        removed: false

      },

      {

        username:"david7",

        joineddate:3333333333,

        removed: false

      },

      {

        username:"william4",

        joineddate:444444444444,

        removed: false

      },

    ]

  },

  {

    id:2,

    name:"stars",

    members:[

      {

        username:"john22",

        joineddate:111111111111,

        removed: false

      },

      {

        username:"david7",

        joineddate:111111111111,

        removed: false

      },

    ]

  }

] }
1 Like

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.

1 Like

Thank you @Prasad_Saya. I will try this. I hope I can.

I tried but it didn’t work. Could you please write a query for the example above? @Prasad_Saya

What did you try? Please post your code.

I am writing according to the example above.

groups.find({$expr:{$eq:['members.username',[ 'john22', 'david7' ]]}})

I had mentioned about using the $setEqualsoperator. Try it, instead of the $eq. The $setEquals is defined as follows, and it is what you are trying:

Returns true if the input sets have the same distinct elements.

I also need help answering this question. Thank you for asking this question.

({$expr:{$eq:['members.username',[ 'john22', 'david7' ]]}})

This should have worked - what was the result when you used this?

Unfortunately, it didn’t work. The blank result has returned. Thank you for your interest, I still haven’t found the solution for this.

Do you have another idea? @Asya_Kamsky

I think I found a solution for this.

groups.find({$and:[{'members.username':{$all:arg.userNameArray}},{'members':{$size:arg.userNameArray.length}}]})

This gives the results I want

tnx

It does with $members.username rather than members.username.

However it does not work if the order is not the same. That is why $setEquals as hinted

is the proper solution.

1 Like

I actually think the solution with $all operator that OP eventually find is better here!

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:2 only.

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.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.