1

Right now, we have a Post collection that the users query based on their locale. The collection looks like this:

Post {
   userId: String
   text: String
   locale: String
   ...
   ...
}

And a User collection that looks like this:

User {
   id: String
   username: String
   locale: String
   ...
   ...
}

Now that the business model changes, we need to make some changes to how the users fetch posts. So, instead of querying only on locale, they will also need to fetch posts based on some other data:

  1. users they frequently chat with
  2. users with 3 or more common interests (this feature is not implemented yet)
  3. users they manually choose to see

Considering data 1 and 3, I came up with the UserData collection, which looks like this:

UserData {
   userId: String
   chatUserIds: Array<String>
   favoriteUserIds: Array<String>
}

The idea behind this collection is that every time the app launches, this collection will be fetched in order to gather the data for the current user, and after that, the Post query will be made like this:

query Post, where locale equals currentUserLocale and userId is contained in joined(chatUserIds, favoriteUserIds), sorted by createdAt descendingly.

As for data no 2, "users with 3 or more common interests", I’ve decided to add to the UserData collection an array of strings, containing the names of the interests the user chose during signup, so it’ll be updated to this:

UserData {
   userId: String
   chatUserIds: Array<String>
   favoriteUserIds: Array<String>
   interests: Array<String> // ["Sports", "TV", ...]
}

Now, this is the part that I'm struggling with; how can I efficiently query the Post collection, to get posts where the poster has 3 or more common interests with the current user?

I thought updating the Post collection to make it have its posters' interests, but I don’t know how to make a query to get posts where the current user has at least 3 matches with each post's interests.

Essentially, this is what I need:

query Post, where locale equals currentUserLocale, userId is contained in joined(chatUserIds, favoriteUserIds) and interests contains at least 3 of currentUserInterests, sorted by createdAt descendingly.

Any idea will be appreciated greatly!!!

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.