Auto increment ID in couchbase lite replication

Hi ,

I am trying implement a solution where i need to create a document Id based on sequence no.
End user can add a document from a web portal,where i am using sync gateways REST api to create document
and they can use mobile app too to create document, where i am using couchbase lite db
i want to make sure that document created from both ways always have unique document id based on a sequence no
As mobile user can create document offline there is no way to check latest sequence available

Is there any work around for this other than using GUID

I am using .net SDK for mobile


Document IDs are always unique in the system. They can be specified by the application using the GetDocument method (the PUT /{db}/{doc} on Sync Gateway) or generated by the database using the CreateDocument method (the POST /{db} on Sync Gateway)

If two documents are created with the same document ID then it’s a conflict and it can be resolved on the client side or on the server side through the changes feed. It’s often easier to resolve the conflict on the client as it avoids the need to maintain an additional App Server.


Yes, I can handle conflicts on client , but how to identify if document is created on device or on web app as both have same document id ,

Basically I want to change the document id (with next doc id in sequence) instead of merging it ,which is created on device as it is not pushed to server yet

You can’t use anything like a sequence number, because Couchbase Mobile is a distributed system with partition tolerance — that means everything still functions when the various computers are offline or unable to communicate.

The only way to manage a distributed counter is to have the computers communicating every time someone needs to update the counter. That won’t work here.

This is why Couchbase Mobile, like Couchbase Server and CouchDB, etc., uses UUIDs: because they provide unique identifiers that can be generated independently.

Thanks, I will go with UUID , still is there any way to add prefix to UUID ?

for example item::<uuid> , category::<uuid>

Sure, just generate the UUID yourself using some platform API, then insert a prefix. Use that to create the doc. (For example, with the REST API use a PUT instead of a POST.)