0

I've been reading many SO posts about specific querying situations and I've hit a roadblock on mine when trying to determine the best approach without going overboard on read requests.

I have an app where a user can follow multiple artists and on the home page it should display the upcoming submissions sorted by date. I want to limit the number of reads to 15, but the way my structure is set up, I cannot limit overall reads but just reads to a specific artist. Let me explain:

For a specific user, I save all artists that a user follows into an array:

user: {
    userUsername: 'jlewallen18',
    userArtistUIDs: [1,4,8,9]
}

To establish a connection to a submission from an artist a user follows, I add the artistUID to the submission:

submission: {
    submissionArtistUID: 4,
    submissionTitle: 'New Album'
    submissionReleaseDate: 1550707200
}

Now the way I find what I need to display on the home page for a specific user is to loop through all the artist id's the user followers and query for submissions with that id. I can then subscribe to that observable using combineLatest from rxjs and receive my results.

this.submission$  = user.userArtistUIDs.map((id) => {
  return this.db.collection$('submissions', ref => ref
    .where('submissionArtistUID', '==', id));
});

This works great, but wont scale well against my read quota, because it queries for every single release from every artist I follow. So if I follow 300 artists, each with 3 releases, I would have 900 reads sent to the app and I'd have to sort and slice the final array to cut it to 15 on the client side.

This is what I'm looking for:

With my list of artistIDs -> query for submissions that contain the artistID in my array -> sort by ascending order -> limit to 15.

Thus my read count would only be 15. I know that for NoSQL there isn't a one trick pony, but I am struggling to figure out the optimal method as there will be more reads than writes on my application.

Jordan Lewallen
  • 1,681
  • 19
  • 54
  • I think **[this](https://stackoverflow.com/questions/46979375/firestore-how-to-structure-a-feed-and-follow-system/52153332)** might help you. – Alex Mamo Feb 16 '19 at 11:13
  • Hey @AlexMamo thanks I saw your answer earlier in the day, and I have this same structure! The fundamental problem I have is figuring out the correct query to only return the most recent submissions FROM artists I follow, without having to return ALL documents from ALL artists I follow and then sorting them afterwards. Does that make sense? – Jordan Lewallen Feb 16 '19 at 19:05

1 Answers1

0

With Firestore there is a limit() argument that can be passed along with the query, see here

Would this work?

this.submission$  = user.userArtistUIDs.map((id) => {
  return this.db.collection$('submissions', ref => ref
    .where('submissionArtistUID', '==', id).limit(15));
});
Lee
  • 141
  • 7