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?
To retrieve the document(s) with
doc_id=10, you need to create a view withdoc_idas a key. Afair, you cannot enforce uniqueness of thedoc_id.Instead of using your
doc_id, you could still use CouchDB's_idfield. 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_idfield, you can create a document with an_idyou 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.