CouchDB Views

CouchDB Views

by 

| March 22, 2022 | in

CouchDB is a pretty amazing technology, but it is a little unique. With many data stores, you just query data. You can do that with CouchDB’s Mango queries, but CouchDB’s primary way to query data is by using views.

Views are pre-created, much like creating any document within CouchDB. After creating your view, you can query it using an HTTP GET request.

You can use the Fauxton CouchDB UI to create a view.

Creating a new CouchDB View

A view has a few components. The first is the “_id“. Everything in CouchDB has an “_id“. The second is a list of views. There could be multiple views (although my first example above has only one). The view has a map function, which is a JavaScript function that can be used to return the data we want. In the example above, all documents with type equal to contact.

To run the query, we need to make an HTTP GET request. For the example above, we would need to make the request to this URL:

http://localhost:5984/DBNAME/_design/view11/_view/all

As an interesting side note, views don’t have to have only one per document. We could have two views on a single document.


Related posts