Pouria
Pouria

Reputation: 431

Efficient way to build a review and like/dislike system in MongoDB

I'm creating a directory website where I want to allow users to be able to like/dislike an entry and be able to leave a review text. I was wondering what's the best way to create the database structure for that.

On a single entry page on the app I need to get these data:

So what do you suggest? How should I connect these three schemas together?

User {
  ...,
  // do I need to referance anything?
}

Entry {
  ...,
  // maybe I should reference Reviews here
}

Review {
  ...,
  liked?: boolean,
  review: string,
  // Entry reference?
}

Could be a better way? Remember that I need to get the count of likes and dislikes too and this happens frequently. So it needs to be very efficient.

Upvotes: 0

Views: 1165

Answers (1)

max meijer
max meijer

Reputation: 69

Thinking about your data structure is very important and knowing what features you are going to implement can help you a big deal. What helps is to break it down in little pieces and try and fit them together.

Likes and dislikes for an entry? 2 simple counters! if you want to load all the reviews for an entry you'll need to know which reviews are about said entry so you'll need a reference. Piecing it together like that will give you a nice clean data structure that you can work with.

Below I've given you a possible implementation of your data structure.

User {
  ...,
  // For quick indexing(at the cost of storage space) 
  // you could keep a list of review references in user
  reviews: string[]
  // Array of objects that tracks whether a user has liked or disliked a certain entry
  likes: [{entry: string, liked: boolean}]

}

Entry {
  ...,
  // Reference to all the reviews
  reviews: string[]
  // Independent counter for likes and dislikes.
  likes: int
  dislikes: int
}

Review {
  ...,
  // Reference to the user
  userId: string
  review: string,
}

Upvotes: 1

Related Questions