Home > OS >  I need to get a specific document, where a document contains exactly two specific values
I need to get a specific document, where a document contains exactly two specific values

Time:07-10

.collection('chats')
        .where('members',
            arrayContains: List.of([user1,user2]))
        .snapshots();

I need to get a specific document, where 'members' contains exactly 'user1' and 'user2' only, and not just one of the values.

Also very important that the order of the values inside the 'members' array/list should be irrelevant..

Because this method using arrayContains is giving me any document that contains either one of these users.

CodePudding user response:

What you're trying to do isn't possible exactly as you've specified. But you can do this if you assume the order of the elements in the array. For that, use a normal isEqualTo filter, providing the exact contents of the array to match. I don't write dart, but it should go something like this:

.where('members', isEqualTo: List.of([user1,user2]))

You can make sure the array always has a consistent order by sorting the items in the array before writing the field, and sorting the array you use in the query.

If the original order of the array is important, then maintain a duplicate field with the same array elements that are always sorted, and query that field instead of the original field.

See also:

  • Related