Migrating Couchbase to MongoDB: Creating a Couchbase View

The JSON data stored in Couchbase Server can be indexed using a view, which creates an index on the data according to the defined format and structure. A view extracts the fields from the JSON document object in Couchbase Server and creates an index that can be queried. A view is a logical structure, and a map function maps the fields of the JSON document object stored in the Couchbase Server to a view.

Optionally a reduce function can also be applied to summarize (or average or sum) the data. In this section we create a view on the JSON document in the Couchbase Server. A map function has the following format.

function(doc, meta)

{

emit(doc.name, [doc.fieldl, doc.field2]);

}

When the function is translated to a map() function, the map() function is supplied with two arguments for each document stored in a bucket: the doc arg and the meta arg. The doc arg is the document object stored in the Couchbase bucket, and its content type can be identified with the meta.type field. The meta arg is the metadata for the document object stored in the bucket. Every document in the data bucket is submitted to the map() function. Within the map() function any custom code can be specified. The emit() function is used to emit a row or a record of data from the map() function. The emit() function takes two arguments: a key and a value.

emit(key,value)

The emitted key is used for sorting and querying the document object fields mapped to the view. The key may have any format such as a string, a number, a compound structure such as an array, or a JSON object. The value is the data to be output in a row or record and it may have any format including a string, number, an array, or JSON. Specify the following function for the mapping from the Couchbase Server bucket to the view. The function first tests if the type of the document is JSON and subsequently emits records with each record key being the document name and each record value being the data stored in the fields of the document object.

Next, create a view in Couchbase Console.

  1. Select Data Buckets default bucket. Subsequently, select View. The Development View tab is selected by default.
  2. Click on Create Development View to create a development view as shown in Figure 7-15.

  1. In Create Development View dialog specify a Design Document Name (_design/dev_catalog) and View Name (catalog_view) as shown in Figure 7-16. The _design prefix is not included in the design document name when accessed programmatically with a Java client. Click on Save.

A development view called catalog_view gets created as shown in Figure 7-17.

  1. We need to edit the default Map function to output a JSON with key/value pairs for the journal, publisher, edition, title, and author fields. Click on Edit to edit the view.
  2. Copy the following function to the View Code Map region.

function(doc,meta) {

if (meta.type == ‘json’) {

emit(doc.name, [doc.journal,doc.publisher,doc.edition,doc.title,doc.author]);

}

}

  1. Click on Save to save the Map function. The catalog_view view including the View Code is shown in Figure 7-18.

7. We need to convert the development view to a production view before we are able to access the view from a Couchbase Java client. Click on Publish as shown in Figure 7-19 to convert the view to a production view.

Source: Vohra Deepak (2015), Pro MongoDB™ Development, Apress; 1st ed. edition.

Leave a Reply

Your email address will not be published. Required fields are marked *