couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Svein Helge Grinden <svein.helge.grin...@gmail.com>
Subject Getting attachments by custom field
Date Wed, 29 Dec 2010 17:38:04 GMT
Hi

I was wondering if there is possible to add a custom field to an attachment.

Scenario:
I have documents with attachments of different types(invoice, contract,
confirmation etc.).
Example:

{
   "_id": "5fdf72a5c03d4b3f89f55e63db002126",
   "_rev": "3-ec0d8a43dbd068be752b0528f1a199e7",
   "type": "order",
   "customerid": "123",
   "orderid": "0001",
   "_attachments": {

       "doc1.txt": {
           "content_type": "text/plain",
           "revpos": 2,
           "length": 10134,
           "stub": true
       },
       "doc2.txt": {

           "content_type": "text/plain",
           "revpos": 2,
           "length": 10134,
           "stub": true
       }

}

Then I would want to list documents by type for a specific order.

1. My first approach was this:

function(doc) {
var invoice = "invoice_";
  if (doc._attachments) {
for (var i in doc._attachments)
{
if(i.substring(invoice.length, 0) == invoice)
{
     emit(doc.orderid, doc);
}
   }
  }
}

The solution above would demand that I name every attachment in
a specific way with the type as the first part of the name.

2. My second approach was to store every attachments as "normal documents
and connect them by key. That would mean having orderid on every attachment.

function(doc) {
  if (doc.type == "invoice")
  {
emit(doc.orderid, doc);
  }
}

This approach would be more RDBMS like.

3. What I was hoping was possible was to have my own custom field on the
attachments so the view would look like this.

function(doc) {
  if (doc._attachments) {
for (var i in doc._attachments)
{
if(i.type == "invoice")
{
     emit(doc.orderid, doc);
}
   }
  }
}

I have tried to add custom fields to an attachment by they are removed on
save.


I would like to hear your opinion about the different approaches and what
you would do to solve this scenario.

Regards,
Svein Helge

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message