Google Firestore - 如何在一次往返中透過多個 ID 取得多個文件?
P粉536532781
P粉536532781 2023-08-27 20:19:08
0
2
418
<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學習者快速成長!