Retrieve documents from CouchDB based on unique field
How can I retrieve document from CouchDB based on its field, not by ID?
T开发者_开发百科he 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 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.
精彩评论