So how can we ensure that email is unique?
So first of all, I'll delete that redundant entry here,
so in my users collection I'll delete one entry and I'll identify the entry by ID by using that objectid
here
and now we can add a unique index. So on users, we can create that index on the email field, we pass some
configuration here where we set unique to true
and now we'll get an error if we try to insert something which exists already.
So now I inserted that index
and now let's try inserting that same email one more time.
Now you see I get an error here
and indeed, I print the error here on the backend server, duplicate key error collection.
So now I get this error
and of course you can handle it in a different way if you want but now we make sure that node duplicate
data for the e-mail can end up. So that is another useful thing which we simply added by adding an index.