Merge resolution at document creation time and not at the time of revisions

With using Couchbase lite, I am having a typical scenario. Where merge would have to happen after document creation and not after update.

A typical user is identified by its information in the document with its email id as the doc _id. User may or may not be registered.

So for example we have three Users. User A (userA@abcd.com) , User B (userB@abcd.com) and User C (userC@abcd.com).

Though app is able to work offline, User A creates a new List A which is shared with userB@abcd.com that is already registered by getting the user’s email from its local contacts.

So it will create a document with the email address in id as u::userB@abcd.com. And the data was also modified 2-3 times. So it will contain rev id starting from 4.

Now as userB@abcd.com is already registered, it will already have a document on server with same id.

When the internet connection is available this tries to sync by getting the revision no, it finds that userB@abcd.com in the local of user A is having higher rev id. So it will override the user’s original data by the data which was aquired by the local contact book.

How can I avoid this situation where documents are generated with the same id at different devices.?

The other thing is is there any way to give 2 different ids to the same document? So that I can identify the same user even offline having different email ids. Like if userB having userB1@abcd.com and userB2@abcd.com but should point to the same user account.

Why does use A need to create the document representing user B? Usually if you wanted to refer to another user you’d just use their username in a property, instead of creating a document for them.

When the internet connection is available this tries to sync by getting the revision no, it finds that userB@abcd.com in the local of user A is having higher rev id. So it will override the user’s original data by the data which was aquired by the local contact book.

Yes, that’s a conflict. Both revisions are available, and the app should look at both of them and save a merged revision to resolve the conflict.

How can I avoid this situation where documents are generated with the same id at different devices.?

There’s nothing wrong with doing it, as long as you’re prepared to handle conflicts. You can avoid it by always using UUIDs for document IDs. Or you can avoid it by not creating documents in a namespace that belongs to another user, according to your schema. (Which is basically what I said at the top – don’t have user A create a doc that belongs to user B.)

The other thing is is there any way to give 2 different ids to the same document?

No. By definition, the docID is the unique identifier of a document in a database.

The thing is user is identified by its email. And I don’t wanted to scan whole contact book of user to get their ids if registered and store them to perfectly point to particular user. So I was finding a way to get it work even when user is offline and create a List.

But, this situation will lead me to more complex things than I thought! I think webhooks can help to link users when that document with email id is synced and can be updated by finding user’s id based on emails.

Sorry for some silly questions, I am a newbie in Couchbase and NoSQL world. Trying to grasp things quickly !