1

I am trying to understand what exactly couchbase view is used for, I have gone through some materials in docs, but the 'view' concept does not settle me quite well. Are views in couchbase analogues to views in view in RDBMS?

https://docs.couchbase.com/server/6.0/learn/views/views-basics.html

A view performs the following on the Couchbase unstructured (or semi-structured) data:

Extract specific fields and information from the data files.

Produce a view index of the selected information.

how does view and index work here, seems there is separate index for view. so if a documents updates are both indexes updated?

https://docs.couchbase.com/server/6.0/learn/views/views-store-data.html

In addition, the indexing of data is also affected by the view system and the settings used when the view is accessed.

Helpful post: Views in Couchbase

kuhajeyan
  • 10,727
  • 10
  • 46
  • 71

1 Answers1

4

You can think of Couchbase Map/Reduce views as similar to materialized views, yes. Except that you create them with JavaScript functions (a map function and optionally a reduce function).

For example:

function(doc, meta)
{
  emit(doc.name, [doc.city]);
}

This will look at every document, and save a view of each document that contains just city, and has a key of name.

For instance, let's suppose you have two documents;

[
key 1 {
   "name" : "matt",
   "city" : "new york",
   "salary" : "100",
   "bio" : "lorem ipsum dolor ... "
},
key 2 {
   "name" : "emma",
   "city" : "columbus",
   "salary" : "120",
   "bio" : "foo bar baz ... "
}
]

Then, when you 'query' this view, instead of full documents, you'll get:

[
key "matt" {
   "city" : "new york"
},
key "emma" {
   "city" : "columbus"
}
]

This is a very simple map. You can also use reduce functions like _count, _sum, _stats, or your own custom.

The results of this view are stored alongside the data on each node (and updated whenever the data is updated). However, you should probably stay away from Couchbase views because:

  • Views are stored alongside the data on each node. So when reading it, data has to be pulled from every node, combined, and pulled again. "Scatter/gather"
  • JavaScript map/reduce doesn't give you all the query capabilities you might want. You can't do stuff like 'joins', for instance.
  • Couchbase has SQL++ (aka N1QL), which is more concise, declarative, and uses global indexes (instead of scatter/gather), so it will likely be faster and reduce strains during rebalance, etc.
  • Are deprecated as of Couchbase Server 7.0 (and not available in Couchbase Capella at all)
Matthew Groves
  • 25,181
  • 9
  • 71
  • 121
  • 1
    https://blog.couchbase.com/couchbase-views-and-better-alternatives-part-1-of-2/ https://blog.couchbase.com/comparing-couchbase-views-couchbase-n1ql-indexing/ – vsr May 24 '22 at 01:16