Search code examples
python-3.xcouchdb

How to run a mango query on a couchdb view?


I use the python package couchdb to access a Apache CouchDB.

My task is to filter documents from the database and be able to select from the filtered database documents with a mango query.

I have defined a view for my database that filters out irrelevant documents. The map function of the view is a sequence of guards (if statements that return nothing if fulfilled). The last statement emits the document with key=id and value=doc:

function(doc) {
  if(...) {
    return
  }
  emit(doc._id, doc)
}

Now I want to use a mango query to select some documents from the view.

But the find method does not exist for views. It only exists for the database object:

Or does it somehow?

And if not, how can I achieve a filter + a mango find with a couchdb?


Solution

  • You cannot perform a mango query on a view. You have to choose between these two options.

    A view as you implement is a way to filter and sort documents in a custom order. Then you can fetch these documents by calling the view and get the exact order you defined.

    For example, you can specify a specific range of documents to fetch (with the view) by using GET / POST /{db}/_design/{ddoc}/_view/{view} endpoint thanks to its parameters (mainly with startkey, endkey, descending, limit and skip).

    But you cannot perform a mango query on a view. Mango queries can only be done on an entire database.

    In your case, you probably should use mango query and put indexes on queryable fields to improve performance.