First of all, store the "added" field in milliseconds using ServerValue.TIMESTAMP
. You can refer to this question to know more about how to do that easily.
So, now that you've stored the time in milliseconds, your "added" field would contain a Long
value. So, you can now perform filtering on your queries to get the data that you need.
Check out this part of the Firebase reference docs for the filtering methods - specially startAt()
and endAt()
.
For example, you want to retrieve the users added in the last 7 days - so 7 days in milliseconds is 86400000. You can simply run a query that is similar to the following to retrieve the users added during the period of "current time in milliseconds" to "current time in milliseconds - 86400000" :-
firebase.database().ref('posts').startAt(current time - 86400000).endAt(current time);