mirror of
https://github.com/jo/couchdb-best-practices.git
synced 2026-05-15 14:16:00 -06:00
[GH-ISSUE #56] How to handle unique Fields in Documents except _id #536
Labels
No labels
pull-request
question
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference: github-starred/couchdb-best-practices#536
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @h4cc on GitHub (Jul 8, 2015).
Original GitHub issue: https://github.com/jo/couchdb-best-practices/issues/56
Common case it to have unique Usernames/Emails in a document. How can be assured with couchdb, that the values of these fields stay unique?
A Idea i found interesting on stackoverflow, lets discuss the approach: http://stackoverflow.com/questions/1541239/unique-constraints-in-couchdb
@jo commented on GitHub (Jul 9, 2015):
I also would have recommend the approach described in that stackoverflow answer but did not try it yet nor have thought about that method in depth.
Do you have a concrete use case?
@h4cc commented on GitHub (Jul 9, 2015):
Have not yet implemented any of these ideas. Will give feedback when i succeded, or failed...
@h4cc commented on GitHub (Jul 30, 2015):
Current state:
I tried having own documents for unique field, like unique usernames for user accounts.
It can be done using a user document containing
{username: alice}and a view usingemit(doc.username, null), but that would not guarantee uniqueness. If that does not need to be strongly enforced, it might be the easiest way to go.A other implementation is using a "unique-doc" for that, like
{_id: user-username-alice, username: alice, user_id: user-1234}. Having such a document per username can guarantee its uniqueness. To query by username then, could either be done doing two request to couchdb, like firstGET /foo/user-username-aliceand thenGET /foo/user-1234. Or a view for the unique usernames doing a "JOIN" could be used likeemit(doc.username, {_id: doc.user_id}), that would be reducing to a single query to a view.What bothered me was having too much documents, whose only use was to ensure uniqueness or N-M relations. Is there a universal hint that says try to reduce the number of documents or rather try to reduce the number of changes to/size of existing ones?