2

When I try to filter my data with IN query it's not showing me the result I am getting a blank array in result I have tried the same thing in the firebase console. I am not getting result there as well.

My data is messages/01fjubNOf8oYODitr1h6 In this path, I have keys - name, message, participants

{
  name: "Pawan arora",
  message: "test",
  participants: ["33", "127"]
},
{
  name: "TEST NAME",
  message: "not working",
  participants: ["114", "127"]
}
{
  name: "TEST DATA",
  message: "new",
  participants: ["114", "33"]
}

I tried in the query to filter data my query is

.collection("messages").where("participants", "in", ["33", "127"])

Here I should get the first object as a result

Firebase database image

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
  • Have a look at the duplicate answers which are valid for `in` as well. Your array is populated with objects. – Renaud Tarnec Jan 07 '21 at 10:58
  • 1
    @RenaudTarnec Maybe there is some misunderstanding to see my data on firebase I have data be like groups/01fjubNOf8oYODitr1h6 In this path, I have keys - name, message, participants Here in groups collection, I want to filter it by participants key – Pawan Arora Jan 07 '21 at 11:53
  • @RenaudTarneci have edit my question please have a look at this again. also i am not able to reopen it can you please repoen it as well – Pawan Arora Jan 07 '21 at 12:01
  • Please share a screenshot of your Firestore database. Question is reopened. – Renaud Tarnec Jan 07 '21 at 12:40
  • @RenaudTarnec I have added a link for the image in my question. please have a look https://i.stack.imgur.com/D0t94.png – Pawan Arora Jan 07 '21 at 13:04
  • I see that you adapted the name of the collection to `messages`. Isn't it working now? Do you see any error? Is there any security rule? – Renaud Tarnec Jan 07 '21 at 13:09
  • @RenaudTarnec NO I have not added any security rule for this collection. Also, I am not seeing any error – Pawan Arora Jan 08 '21 at 04:52
  • How do you log error? – Renaud Tarnec Jan 08 '21 at 08:03
  • @RenaudTarnec i am logging any error for now. but I am trying this to filter in cloud firestore console it's not working there as well.For ref check screenshot https://prnt.sc/wiuv03 – Pawan Arora Jan 08 '21 at 08:48

1 Answers1

2

You need to use array-contains-any operator instead when filtering arrays.

Try this:

.collection("messages").where("participants", "array-contains-any", ["33", "127"])

For details check this blogpost.

Emil Gi
  • 1,093
  • 3
  • 9
  • Actually, my requirement is to get where a particular id get matched not like where it contains For example, I have 3 messages with participants id [33, 127] and [114, 127] and [33, 114] If I applied filter with id 33, 127 then i should only [33, 127] Can anyone please help me here which filter i should use now – Pawan Arora Jan 11 '21 at 11:45
  • 2
    Oh, i see, in this case have you tried double brackets? Like `.collection("messages").where("participants", "in", [["33", "127"]])`. – Emil Gi Jan 11 '21 at 11:49
  • 1
    Thanks :) Wooah!! It's working for me but I have to reorder my array while creating and getting the collection Can you please tell me one thing is it documented anywhere? Because I am not finding this method anywhere in docs – Pawan Arora Jan 12 '21 at 12:36
  • 1
    I mean it just looks not for string objects but for arrays. In documentation strings are just for example, I believe you can filter by any object type. – Emil Gi Jan 13 '21 at 10:59
  • 1
    Hey, @Emil Gi with your above solution with two brackets I got a problem here in one more scenario. For example, I have 4 messages with participants id [33, 127] and [114, 127] and [33, 114, 127] and [33, 114] If I applied filter with id 33, 127 then I should get two results here [33, 127], [33, 114, 127] but I am receiving only one here [33, 127] Can you please help me to achieve this one? – Pawan Arora Jul 12 '21 at 08:42