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

前端 未结 11 1800
余生分开走
余生分开走 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: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(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.

提交回复
热议问题