繁体   English   中英

有什么方法可以从 firebase firestore 中查询 where 来查找文档

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

我有一个令牌集合,其中每个文档都使用自动 ID 创建,但我在文档中存储了一个 tokenId,现在我想搜索具有特定 tokenId 的单个文档 我如何在我的代码中实现 where 查询

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

首先,您应该使用collection()而不是doc()来创建CollectionReference 然后您可以使用query()where()构建所需的 查询,如下所示:

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")
}

还结帐:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM