Search code examples
couchbasecouchbase-view

Filter data in couchbase View


When i write view in couchbase to return whole doc it also return _sync":{} data, is there any way to remove this data from response.

here is my view function:-

function map(doc, meta) {
    if (doc.type == 'user' && doc.user_id) {
        emit(doc.user_id, doc);
    }
}

Solution

  • first off, you shouldn't ever have to emit the whole doc. This makes the index bigger on disk, and it's redudant since you can get the whole doc easily from a view row (the doc id is always included, and the SDKs will transparently fetch it for you usually).

    in your case though, you may actually need that second part of emit. Select the attributes you are interested in and emit them in an array (like emit(doc.user_id, [doc.attributeA, doc.attributeB])), thus effectively "filtering out" _sync.

    only problem is that if later on you add an attributeC to your users, it won't automatically be included (so it filters out attributes not explicitly listed in the map function). does that make sense?