根据ID或属性迭代Firestore Android集合 [英] Iterating Firestore Android collection on basis of ID or attribute

查看:207
本文介绍了根据ID或属性迭代Firestore Android集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我想迭代一个从集合文档ID开始的集合。

I want to iterate a collection which starts at collection document ID.

是否有类似下面的方法让我们以id或属性ID读取数据?

Is there any method like below which let us read data start at an id or an attribute id?

 db.collection("questions").startReadingDocumentAt("02cubnmO1wqyz6yKg571 ").limit(10).get() 

db.collection("questions").startReadingWhereEqualTo("questionID","02cubnmO1wqyz6yKg571 ").limit(10).get()

我知道我可以根据Id 读取DocumentSnapShot,然后可以开始使用lastVisible项进行迭代。但它花了我两个读操作。它可以在一个完成。

I know I can read that DocumentSnapShot on basis of Id and can then start iterating with lastVisible item. but it cost me two read operation. Can it be done in one.

Firestore-root
    |
    --- questions (collections)
    |     |
    |     --- 02cubnmO1wqyz6yKg571 (questionId document) // myID and my questionID is same
    |            |
    |            --- questionId: "02cubnmO1wqyz6yKg571"
    |            |
    |            --- title: "Question Title"
    |            |
    |            --- date: August 27, 2018 at 6:16:58 PM UTC+3
    |            |

我已经离开 通过此

I already have gone through this

解释

我不想做 RecyclerView分页。我想在每次活动开始时加载下一个10个问题。所以我以相同的方式调用查询,但它需要我保存 DocumentSnapShot 。所以我决定保存questionID并根据它读取数据...

I don't want to do RecyclerView pagination. I want to load next 10 question each time an Activity starts. So I call the query in the same manner but it requires me to save the DocumentSnapShot. So I decided to save the questionID and read data on basis of it...

推荐答案

没有:

startReadingDocumentAt("02cubnmO1wqyz6yKg571")

startReadingWhereEqualTo("questionID","02cubnmO1wqyz6yKg571")

在firestore中,但根据您的数据库架构,要解决此问题,您可以使用以下查询:

In firestore but according to your database schema, to solve this, you can use the following query:

db.collection("questions").
    whereGreaterThanOrEqualTo("questionID","02cubnmO1wqyz6yKg571")
    .limit(10)
    .get()
    addOnCompleteListener.(/* ... */)

更多信息这里

创建并返回一个新的Query,其中包含文档必须包含指定字段的附加过滤器,且值应大于或等于指定值。

Creates and returns a new Query with the additional filter that documents must contain the specified field and the value should be greater than or equal to the specified value.

这篇关于根据ID或属性迭代Firestore Android集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆