couchbasecouchbase-view

Getting all subitems from all documents with a Couchbase map/reduce view


I have Couchbase documents with structure like this:

{
  "subscriberIds": [
    {
      "type": "END_USER",
      "identity": "00000000223"
    }
  ],
  "userDataId": "SUB-00000000223",
  "status": "ACTIVATED",
  "subscriptions": [
    {
      "id": "Something1",
      "attributes": [
        {
          "value": "Active",
          "name": "Status"
        }
      ],
      "priority": "1",
      "subscriptionStartDate": somedate,
      "productShortName": "Something1"
    },
    {
      "id": "Something2",
      "attributes": [
        {
          "value": "Active",
          "name": "Status"
        }
      ],
      "priority": "1",
      "subscriptionStartDate": somedate,
      "productShortName": "Something2"
    }
  ],
}

And I'm trying to write a view to get all the 'subscriptions' from all documents in bucket as:

{"total_rows":900,"rows":[
{"id":"Something1","key":null,"value":"00000000223"},
{"id":"Something2","key":null,"value":"00000000223"},
...

but, I can't get nested item from doc

function (doc, meta) {
  for (var i in doc.subscriptions) {
    emit(doc.subscriptions.id, doc.id);
  }
}

I know this is possible, but apparently I do not fully understand the concept of views.


Solution

  • for (var i in doc.subscriptions) {
        emit(doc.subscriptions[i].id, doc.id);
    }