MJ Nkunim
MJ Nkunim

Reputation: 186

firestore Check if id exists against an array of ids

I developing a simple chat applicaiton for my website using firebase firestore. where every chat session has an id

provided i have an array of ids

chat_sessions = ["q93XhadA9QQLu6X8yfZB", "YXEYISEI8b2iixCcP3VO", "GXrSZbtrmN5NcrtvjFYp"]

I want to get all document whose id is equal to any of the id's in the chat_sessions object using the code below.

return this.afs
    .collection('chats', ref => ref.where('uid','in_array',chat_sessions)).snapshotChanges...

but I am not getting any results.

I come from a PHP/MYSQL background the PHP equivalent of what i am trying to achieve will be sth like this in PHP

if(in_array(uid,chat_sessions)){
      get(doc/uid)
      }

can anyone help with the right query where we check for document id against a list of ids in an array? Thank You!

Upvotes: 3

Views: 10719

Answers (5)

Leonardo Barbosa
Leonardo Barbosa

Reputation: 1396

It's 2023 and today you can simply do:

import { doc, documentId, onSnapshot, query, where } from 'firebase/firestore'

const partnerCandidatesQuery = query(
  usersCollection,
  where('role', '==', 'candidate'),
  where(
    documentId(),
    'in',
    candidateIds.map((id) => doc(usersCollection, id)),
  ),
)

Upvotes: 1

Lodovico Giberti
Lodovico Giberti

Reputation: 1

UPDATE

Hi, firebase did some update recently, so for do it I found out this method

`

const [products, setProduct] = useState([]);
const ids = ['H11LlJsh3sObwORZhA0b','om9m0lU9HYWyOJZKvEdi','1AoHyHuSFcF01zoyXyTD','6xoBlxsRXUoyzBUcWl0F',
'GJqthlmBGZaFAJqtC2jK','QNT3PxMfhNGg1RZnuqcq','RZgGoFZHyDAYaVZJWxGk','g4UO5P0EgtEqJnawwhXX','gyrZm8p0cEgJdDvTuB1g','mrscldfeYlkaSF151MpI',]

useEffect(() => {
    const saveFirebaseTodos = [];      
    ids.forEach((element) => {
    fetchMyAPI()
    async function fetchMyAPI() {
        const q = query(collection(db, "a"), where('__name__', '==', element));
        const querySnapshot = await getDocs(q); 
        querySnapshot.forEach((doc) => {
        saveFirebaseTodos.push(({id: doc.id, ...doc.data()}));
        /*console.log(doc.id, " => ", doc.data());*/
        if (ids.length == saveFirebaseTodos.length) {
            setProduct(saveFirebaseTodos) 
            }
        });
    }})

}, [])`

In this way, you can check how many element in arrays you want (bypassing 10 elements limit of firebase).

Hope can help anyone :D

Upvotes: 0

Rohit Kumar
Rohit Kumar

Reputation: 758

I found something else on firestore i.e "array-contains-any" for this case.

Maybe it's updated now.

Firestore condition

Upvotes: 3

MJ Nkunim
MJ Nkunim

Reputation: 186

Thank you @frank van Puffelen. You were almost right. I should have used in instead of in_array

ref.where(firebase.firestore.FieldPath.documentId(),'in_array',chat_sessions)

did not work. Instead I replaced in_array with in :

ref.where(firebase.firestore.FieldPath.documentId(),'in',chat_sessions)

This worked! Thank you

Upvotes: 10

Frank van Puffelen
Frank van Puffelen

Reputation: 598603

Your query is:

ref.where('uid','in_array',chat_sessions)

This checks a field called uid in each document against the values of the chat_sessions.

It seems that instead you want to the check the ID of each document against the array, which you can do with:

ref.where(firebase.firestore.FieldPath.documentId(),'in_array',chat_sessions)

Upvotes: 7

Related Questions