• Home
  • Portfolio
  • Articles
  • Pricing
  • About
  • Contact

Avoiding duplicate reviews per user

Oct 7th, 2019

Alana Ma

mongoavoid duplicates

In our model, we want to make sure that there can only be one review for each business per user. This makes sense and to implement this is very easy.

ReviewSchema.index({tour: 1, user: 1}, {unique: true});

That is all you need to do as that insures the combined tour and user must be unique. The simply sorts the tour and user. Replace 1 with -1 to sort reversed.