views:

129

answers:

1

How can I retrieve document from CouchDB based on its field, not by ID?

The problem is, documents in my system should have numeric IDs, so I add a field called doc_id to saved documents. Native CouchDB ids are too long for me.

How can I retrieve document with doc_id = 10, for example?

+3  A: 

To retrieve the document(s) with doc_id=10, you need to create a view with doc_id as a key. Afair, you cannot enforce uniqueness of the doc_id.

Instead of using your doc_id, you could still use CouchDB's _id field. Iirc, you do not have to leave it to CouchDB to assign a value to _id. If you do not like the UUIDs CouchDB uses for the _id field, you can create a document with an _id you specify.

You need to be careful with that, esp. in a distributed setup. If you end up with different documents (on different nodes) having the same _id, CouchDB might consider them to be different versions of the same document.

titanoboa
Thanks for the answer :) The connector I'm using seems to have problem with views, but I'll try this thing with custom IDs.
Kuroki Kaze
Documents with the same `_id` **are** different versions of the same document.
jhs
Worked like charm, thanks :)
Kuroki Kaze