0

How can i write condition in Couchbase views to filter deleted collection from result.

function (doc, meta) {
  if(doc.type=='folder'){
    emit(doc.folder_id, doc);
  }
}

Here is my simple view, It gives some deleted folders in json response.

rash111
  • 1,307
  • 4
  • 19
  • 35

1 Answers1

0
  1. Keep in mind that views are only eventually-consistent. They dont return real-time data. It can take a little time for views to actually reflect the data in CB.

  2. Specifically, it takes up to a minute for deleted items to get removed from CB view results. This is somewhat configurable.

  3. Important : dont emit "doc" in your views - that's wasteful (since it stores another copy of each document in CB).

  4. Instead, you can usually request that the document is attached to the result (in python its called "include_doc" - http://pythonhosted.org/couchbase/api/views.html). This can help you with the problem at hand: if you request that documents are included with the view results, then if the document has been deleted, it would simply not return it - telling you that its already been deleted.

    1. Alternatively, if you dont have 'include_doc' in your client, you could do it manually: get the view results (just the list of document ids) and for each document, perform "get" to retrieve the data. if you can't find the doc, its been deleted. (unlike views, 'get' actions are fully consistent - they reflect the data that was written to disc).
FuzzyAmi
  • 7,543
  • 6
  • 45
  • 79
  • it result deleted items after indexing – rash111 Aug 21 '15 at 11:51
  • did my answer help you? – FuzzyAmi Aug 21 '15 at 13:14
  • One small issue with the answer, deleted items do not take a minute to get deleted from views. It is important to understand how views are updated and how they work with expiries. You should have a look over [document expiration](http://docs.couchbase.com/developer/dev-guide-3.0/doc-expiration.html) in the developer guide and [Stream-based views](http://docs.couchbase.com/admin/admin/Views/views-index-updates.html) in the manual. – Paddy Aug 21 '15 at 15:49
  • You're right - I misread 60 seconds when in fact its once an hour for TTL'd docs: "Couchbase Server does lazy expiration, that is, expired items are flagged as deleted rather than being immediately erased. Couchbase Server has a maintenance process, called the expiry pager that periodically looks through all information and erases expired items. This maintenance process runs every 60 minutes" – FuzzyAmi Aug 21 '15 at 16:21