Google Firestore - how to get document by multiple ids in one round trip?

前端 未结 11 1814
余生分开走
余生分开走 2020-11-21 22:49

I am wondering if it\'s possible to get multiple documents by list of ids in one round trip (network call) to the Firestore.

相关标签:
11条回答
  • 2020-11-21 23:34

    This doesn't seem to be possible in Firestore at the moment. I don't understand why Alexander's answer is accepted, the solution he proposes just returns all the documents in the "users" collection.

    Depending on what you need to do, you should look into duplicating the relevant data you need to display and only request a full document when needed.

    0 讨论(0)
  • 2020-11-21 23:36

    if you're within 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])}`);
    * });
    */
    

    This is specifically for the server SDK

    UPDATE: "Cloud Firestore [client-side sdk] Now Supports IN Queries!"

    https://firebase.googleblog.com/2019/11/cloud-firestore-now-supports-in-queries.html

    myCollection.where(firestore.FieldPath.documentId(), 'in', ["123","456","789"])

    0 讨论(0)
  • 2020-11-21 23:37

    They have just announced this functionality, https://firebase.googleblog.com/2019/11/cloud-firestore-now-supports-in-queries.html .

    Now you can use queries like, but mind that the input size can't be greater than 10.

    userCollection.where('uid', 'in', ["1231","222","2131"])

    0 讨论(0)
  • 2020-11-21 23:38

    In practise you would use firestore.getAll like this

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

    or with promise syntax

    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())))
    }
    
    0 讨论(0)
  • 2020-11-21 23:42

    Here's how you would do something like this in Kotlin with the Android SDK.
    May not necessarily be in one round trip, but it does effectively group the result and avoid many nested callbacks.

    val userIds = listOf("123", "456")
    val userTasks = userIds.map { firestore.document("users/${it!!}").get() }
    
    Tasks.whenAllSuccess<DocumentSnapshot>(userTasks).addOnSuccessListener { documentList ->
        //Do what you need to with the document list
    }
    

    Note that fetching specific documents is much better than fetching all documents and filtering the result. This is because Firestore charges you for the query result set.

    0 讨论(0)
提交回复
热议问题