如何在Firebase中检索数据范围 [英] How to retrieve range of data in firebase
问题描述
我有以uuid为键的用户数据.我想向每个用户发送邮件,但是我不想一起向他们发送邮件,所以我想检索用户范围.
I have user's data which have uuid as their key. I want to send mail to each user, But I don't want to send them mail all together so I want to retrieve range of user.
例如,我现在有1000位用户,我想发送邮件到1-100位用户,然后是101-200位,201-300位,依此类推.我怎样才能做到这一点?我已经看过 startAt()
和 endAt()
函数,但是我的问题是我不知道开头和结尾的用户密钥.因此,我将无法通过这种方式检索距离.
For example, I have 1000 user's now I want to send mail to range 1-100 user then 101-200 , 201-300 and so on. How can I achieve this?
I have seen startAt()
and endAt()
functions, but my question is I don't know user key at the beginning and at the end. So I won't be able to retrieve range from that way.
推荐答案
要获取前100个用户,您需要在以下位置进行查询:
To get the first 100 users you'd do a query on:
query = ref.orderByKey().limitToFirst(100)
然后,在处理用户时,请跟踪已处理的最后一个密钥:
Then as you process the users, keep track of the last key you've processed:
vast lastSeenKey;
query.once("value", function(snapshot) {
snapshot.forEach(function(userSnapshot) {
lastSeenKey = userSnapshot.key;
});
});
然后,要加载接下来的100个用户,请使查询从您看到的最后一个键开始:
And then to load the next 100 users, you make the query start at the last key you've seen:
query = ref.orderByKey().startAt(lastSeenKey).limitToFirst(101)
您会注意到,我们在此处检索了一个额外的项目,因为此查询中的第一个用户将与上一个查询中的最后一个用户相同.
You'll note that we retrieve one extra item here, since the first user in this query will be the same as the last user in the previous query.
这篇关于如何在Firebase中检索数据范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!