在 <= V8 Firestore API 中,我可以查询一组文档并使用如下代码提取字段子集:
const membersRef = db.collection('members');
const snapshot = await membersRef.select('First', 'Last').get();
使用新的模块化 API,我可以执行以下操作:
import { collection } from 'firebase/firestore';
const membersRef = collection(db,'members');
// how to select only First and Last from members ?
我找不到 select() 函数来指定新 api 中的字段。如何使用 V9 模块化 API 来完成此操作?
我知道我可以读取整个集合并使用 .map()/.forEach 来减少字段,但我有一些大字段,我宁愿不查询,除非我需要它们用于特定文档。使用 select 让服务器仅提取所需的字段很有用。
最佳答案
Firebase 的客户端 SDK 没有 select
方法/函数,但始终检索完整文档。所以 v8 CollectionReference 中不存在 select
方法或Query类,也不作为 v9 模块化 SDK 中的顶级函数存在。
Node.js 的 Admin SDK 中有一个 select()
方法,但该方法仍然以 method on CollectionRefence 的形式工作。据我所知。
关于node.js - 如何使用 firestore 模块化 api 选择特定字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70418412/