0

I am having couchbase report documents stored in below format:

{
   "agree_allowed":true,
   "assigned_by":"",
   "assigned_to":"",
   "closed":[

   ],
   "comments_allowed":true,
   "details":"Test",
   "email":"",
   "status":"In Progress",
   "subscribed":{
      "user_cfd29b81f0263a380507":true,
      "user_cfd29b81f0263a380508":true,
      "user_cfd29b81f0263a380509":true,
      "user_cfd29b81f0263a3805010":true
   },
   "summary":"Test",
   "time_open":0,
   "timestamp":"2015-07-17T15:34:30.864Z",
   "type":"report",
   "user_id":"user_cfd29b81f0263a380507",
   "username":"test17"
}

json contain subscribed filed, it is list of user_id who follow reports. Problem is i have to emit report document if subscribed field contain user_id, if i pass user_id ='user_cfd29b81f0263a380507' pass as key parameter. i am wondering how can use user_id to compare in view

here is the code i write:-

function map(doc, meta) {
        if (doc.type == 'report' && doc.subscribed) {
            for (var user_id in doc.subscribed) {
                emit(doc.user_id, doc);
            }

        }
    }

but it didn't return expected result. Can anybody help.

rash111
  • 1,307
  • 4
  • 19
  • 35
  • Can you update the question with the output you got out? – Paddy Jul 29 '15 at 17:48
  • 1
    not really sure what you're trying to achieve. perhaps you can give an example? also - dont emit the entire doc as value. Either emit its name (and later get it) or use the "attach doc" option to attach the document to the result. By emitting the doc you're wasting storage space. – FuzzyAmi Jul 29 '15 at 19:27
  • i want to all report data who subscribed that report. so i just change emit like this emit(subscriber, doc). Is this ok? – rash111 Jul 30 '15 at 12:03

1 Answers1

1

If I understand your question I think you want the ability to query the users who have subscribed.

If that is the case the view code is wrong it is submitting doc.user_id and not user_id, which is the variable you assign values to in the loop but never use. In any case I think it would be better to use a different names to avoid confusion.

function map(doc, meta) {
        if (doc.type == 'report' && doc.subscribed) {
            for (var subscriber in doc.subscribed) {
                emit(subscriber);
            }
        }
    }

To query the users who have subscribed you would use key=user_cfd29b81f0263a380507. The result would be:

{
  "total_rows": 4,
  "rows": [
    {
      "id": "docs",
      "key": "user_cfd29b81f0263a380507",
      "value": null
    }
  ]
}
Paddy
  • 1,195
  • 9
  • 16
  • you got my question. But i want to all report data who subscribed that report. so i just change emit like this emit(subscriber, doc). Is this ok? in json response i get _sync data and it is to much, is there any way so i can remove _sync data from result? – rash111 Jul 30 '15 at 07:28
  • W.R.T emit(subscriber,doc) you should see @FuzzyAmi comment in the question. Emitting the whole doc will slow things down as the load increases. The SDK have helper functions to get the whole document through the managed cache. Which will be much faster. Which SDK are you using? – Paddy Jul 30 '15 at 10:58
  • i didnt use any SDK, i write views and access through python code, is it best approach? or should i use Python SDK – rash111 Jul 30 '15 at 11:59