Google Firestore - 如何在一次往返中通过多个 ID 获取多个文档?
P粉536532781
P粉536532781 2023-08-27 20:19:08
0
2
414
<p>我想知道是否可以在 Firestore 数据库的一次往返(网络调用)中通过 ID 列表获取多个文档。</p>
P粉536532781
P粉536532781

全部回复(2)
P粉633075725

实际上,您会像这样使用 firestore.getAll

async getUsers({userIds}) {
    const refs = userIds.map(id => this.firestore.doc(`users/${id}`))
    const users = await this.firestore.getAll(...refs)
    console.log(users.map(doc => doc.data()))
}

或者使用 Promise 语法

getUsers({userIds}) {
    const refs = userIds.map(id => this.firestore.doc(`users/${id}`))
    this.firestore.getAll(...refs).then(users => console.log(users.map(doc => doc.data())))
}
P粉860370921

如果您在 Node 中:

https://github.com/ googleapis/nodejs-firestore/blob/master/dev/src/index.ts#L978

/**
* Retrieves multiple documents from Firestore.
*
* @param {...DocumentReference} documents - The document references
* to receive.
* @returns {Promise<Array.<DocumentSnapshot>>} A Promise that
* contains an array with the resulting document snapshots.
*
* @example
* let documentRef1 = firestore.doc('col/doc1');
* let documentRef2 = firestore.doc('col/doc2');
*
* firestore.getAll(documentRef1, documentRef2).then(docs => {
*   console.log(`First document: ${JSON.stringify(docs[0])}`);
*   console.log(`Second document: ${JSON.stringify(docs[1])}`);
* });
*/

这是专门针对服务器 SDK 的

更新:Cloud Firestore 现在支持 IN 查询!

myCollection.where(firestore.FieldPath.documentId(), 'in', ["123","456","789"])
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!