Alex
Alex

Reputation: 723

Firebase: How do I limit number of child objects in a query for its parent?

In my Firebase I have a users collection in which each user has a child "posts" that contains multiple posts (like Facebook). On each user page I load the posts all in one query for the user object. However over time the number of posts will become very large.

  1. What is the "right" way to limit the number of posts that come without making a separate call (or is that the only way)? I plan to implement infinite scroll.
  2. Generally speaking is it bad practice to make multiple calls with an Angular/Firebase stack? Can someone direct me to a "best practices" resource?

Thanks a lot for your help!

Edit

For the user collection it looks thus

users : { 
  user1 : { posts : [<post>], username : <username> ... }, 
  user2 : { posts : [<post>], username : <username> ... }, 
  ... 
}

Upvotes: 1

Views: 570

Answers (1)

Robert Horvick
Robert Horvick

Reputation: 4036

You can limit the number of items you take using the limitToFirst and limitToLast functions.

To implement pagination, though, you'll might want to use startAt and endAt. You would combine it with once to only get the paginated data once.

In fact, Firebase has an example of pagination using startAt, limitToFirst and once.

Upvotes: 1

Related Questions