is there any way to find a document from firebase firestore with the query of where

Viewed 155

I have a collection of tokens in which each document create with auto id but I store a tokenId in the document and now I want to search a single document which has specific tokenId How can I implement where query in my this code

const docRef = doc(db , "tokens")
const data= await getDoc(docRef);
1 Answers

First, you should use collection() instead of doc() to create a CollectionReference. Then you can build the required Query using query() with where() as shown below:

import { collection, getDocs, query, where } from "firebase/firestore"

const colRef = collection(db , "tokens")

const qSnap = await getDocs(query(colRef, where("tokenId", "==", "TOKEN_VALUE")));

if (qSnap.size) {
  const data = qSnap.docs[0].data();
} else {
  console.log("No token found")
}

Also checkout:

Related