
Question:
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.
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.
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;
});
});
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.