user187676
user187676

Reputation:

Generate document ID server side

When creating a document and letting Couch create the ID for you, does it check if the ID already exists, or could I still produce a conflict?

I need to generate UUIDs in my app, and wondered if it would be any different than letting Couch do it.

Upvotes: 0

Views: 198

Answers (3)

SingleNegationElimination
SingleNegationElimination

Reputation: 156128

You can and should give a document id, even when using the bulk document interface. Skipping that step makes the problem of resubmitted requests creating duplicate documents even worse. On the other hand, if you do assign ID's, and part of the request reaches couchdb twice (as in the case of a reconnecting proxy), then your response will include some conflicts, which you can safely ignore, you know the conflict was from you, in the same request

Upvotes: 0

Stefan Kögl
Stefan Kögl

Reputation: 4733

As Kxepal already mentioned it is generally not recommended to POST a document without providing your own _id.

You could, however, use GET /_uuids to retrieve a list of UUIDs from the server and use that for storing your documents. The UUIDs returned will depend on the algorithm that is used, but the chance of a duplicate are (for most purposes) insignificantly small.

Upvotes: 1

Kxepal
Kxepal

Reputation: 4679

Use POST /db request for that, but you should be aware the fact that the underlying HTTP POST method is not idempotent, and a client may automatic retry it due to a problem some networking problems, which may create multiple documents in the database.

Upvotes: 2

Related Questions