How to enforce unique values in Collections?


#1

I’m collecting Member Registrations, how do I go about enforcing unique User_Names and email_addresses in a collection?


#2

You could add a check to the read permissions, but they are hard to understand for complicated checks.

I just wrote a check for completeness:

But I would recommend to use the collections.save.before trigger. It’s easier to debug and it is easier to understand, which variables are available to modify the data directly or to give suggestions for available user names.

Have a look at UniqueSlugs addon or an older, much simpler version, to get some inspiration.


#3

Thanks, I will.

It seems that parsing (potentially) a whole lot of json strings to eliminate a duplicate is not going to be very efficient? And, it would need to be done client side and server side to ensure data validity??

I’m not familiar with Mongo to know if it offers more efficient ways to achieve this?


#4

It seems that parsing (potentially) a whole lot of json strings to eliminate a duplicate is not going to be very efficient?

I don’t have experience with very large data sets, but the cockpit find and findOne functions are just wrappers for database requests. Use filters and fields filters to keep your responses small. Just compare, if an entry exists in the database. If the response is null, deny the request.

And, it would need to be done client side and server side to ensure data validity??

You can validate only server side and send responses to the client. So you don’t have to validate client side necessarily.

I’m not familiar with Mongo to know if it offers more efficient ways to achieve this?

I never used MongoDB - can’t help here.