1

I have a design document in CouchDB. I've set up views and filters.

{
  "_id": "_design/my_index_id",
  "_rev": "17-fa5c543fcc80f4420aa98d58f7a07130",
  "views": {

    "jobsbyid": {
      "map": "function (doc,req) {if  (doc.type === 'job') {emit(doc.id);}}"
    }
  },
  "filters": {
    "myfilter": "function (doc, req) {return req.query.type === 'job'}"
}

What's the different between views and filters. In terms of performance, use cases and usage. When to use Views and when to use filters?

Alamgir Qazi
  • 763
  • 1
  • 11
  • 25

1 Answers1

4

In CouchDB you have different filtering options for the replication process. All of them are documented here CouchDB filtering options

About filtering, You should have in cosideration that filtering is one of the most expesive operation in CouchDB that could drive you into some performance degradation problems as long the database grows. You can check this answer Filtered Sync between CouchDB and PouchDB

The usage of filters or views are almost the same in terms of performance as they are filtering the whole database in each filtering request. This is stated in the doc

Using _view filter doesn’t queries the view index files, so you cannot use common view query parameters to additionally filter the changes feed by index key. Also, CouchDB doesn’t returns the result instantly as it does for views - it really uses the specified map function as filter.

Moreover, you cannot make such filters dynamic e.g. process the request query parameters or handle the User Context Object - the map function is only operates with the document.

The advantaje of the use of views for filtering is that you are reusing map functions for filtering.

So use cases of both approaches are very similar except that the filters may access to the query params or the security context.

Juanjo Rodriguez
  • 2,103
  • 8
  • 19
  • thankyou! that was very helpful. In my case, I want to get documents based on some logic (e.g lets say get those documents that have field age: 30). For this case, CouchDB Selector cannot work right? and my only option is using a Filter and passing the id 30 via req.query? – Alamgir Qazi Sep 25 '18 at 08:30
  • Selectors are valid in your case. Selectors are provided by the client not defined in design documents, so you can compose the conditions in the client and then pass them to the db replication configuration. – Juanjo Rodriguez Sep 25 '18 at 08:38