Prolog

The previous article provided details on how to migrate a data set from MongoDB to Couchbase. This article will show you how to use the Couchbase SDK to access the data via a Java console application. Code snippets show how to connect to the Couchbase cluster, perform key/value operations, and execute secondary lookups via N1QL queries side-by-side with corresponding code to do the same with the Mongo Java SDK.

All code from this blog is available in the following Git repository: mongodb-to-couchbase.

Prerequisites

A Couchbase cluster containing the data set as per the details in the previous article.

Create an Application User

Before a client (application) can connect to the Couchbase cluster you need to define an application user that will be used for authentication by the client. Couchbase Role-Based Access Control allows you to define users and assign appropriate roles to them. Use the web console to create an application user named mflix_client as follows.

Go to the Security section in the web console and click ADD USER:

mongo-to-couchbase-01-user-list

Configure the mflix_client user as follows and click Add User:

  1. Username: mflix_client
  2. Password: password (or any password of your choosing).
  3. Verify Password: same as Password value above.
  4. Roles: Expand the sample_mflix section and select Application Access. Users with this role have full read and write access to all data in the sample_mflix bucket. This user cannot access the web console and is intended only for application access.

mongo-to-couchbase-02-new-user

In the Security section you will see the new mflix_client user:

mongo-to-couchbase-03-user-list

Create Indexes for N1QL Queries

Secondary Indexes in Couchbase support the efficient execution of queries (or secondary lookups) just like indexes in MongoDB. Code samples in this article execute N1QL queries that use two indexes you need to create. Secondary indexes are created by executing N1QL queries so go to the Query section in the web console:

mongo-to-couchbase-04-query-workbench

The first index is on the name attribute of all comment documents in the sample_mflix bucket. Enter the following N1QL statement in the Query Editor:

mongo-to-couchbase-05-create-idx1

Click Execute and after a few moments the index creation will be complete:

mongo-to-couchbase-06-idx1-created

The second index is on the year, imdb.rating, & title attributes of all movie documents in the sample_mflix bucket. Enter the following N1QL statement in the Query Editor:

mongo-to-couchbase-07-create-idx2

Click Execute and after a few moments the index creation will be complete:

mongo-to-couchbase-08-idx2-created

Go to the Indexes section in the web console to verify that indexes idx1 and idx2 exist:

mongo-to-couchbase-09-indexes

Convert MongoDB API calls to Couchbase API calls

The sample code for this article uses the Couchbase & MongoDB Java SDKs and is provided as an example of how to use some of the SDK APIs. Refer to the following links for the full Couchbase SDK documentation for your language:

Connect to Couchbase Server

In order to access cluster resources, clients must authenticate by passing appropriate credentials to Couchbase Server. The sample code uses the mflix_client application user credentials created above to authenticate.

The following code sample connects to the Couchbase cluster running on the specified node, authenticates with the mflix_client user credentials – mflix_client/password (or the password you chose above) – and opens the sample_mflix bucket.

Couchbase

MongoDB

Retrieve a Document by ID

Use the Bucket.get() method to retrieve full documents by ID. The following code sample retrieves two documents from the sample_mflix bucket.

Couchbase

MongoDB

Insert a New Document

Use the Bucket.insert() method to create a new document with the specified ID & content if it does not already exist. The following code sample inserts this document in the sample_mflix bucket:

Couchbase

MongoDB

Insert Multiple New Documents

Batching operations allows you to make better utilization of your network and speed up your application by increasing network throughput and reducing latency. Batched operations work by pipelining requests over the network. When requests are pipelined, they are sent in one large group to the cluster. The cluster in turn pipelines responses back to the client. When operations are batched, there are fewer IP packets to be sent over the network.

The following code sample uses this approach to insert two new documents into the sample_mflix bucket.

Couchbase

MongoDB

Update an Existing Document

Use the Bucket.replace() method to update an existing document with the specified ID only if it already exists. Couchbase supports sub-document operations which can be used to efficiently access parts of documents. Sub-document operations may be quicker and more network-efficient than full-document operations because they only transmit the accessed sections of the document over the network. Full-document and sub-document operations are atomic, allowing safe modifications to documents with built-in concurrency control.

The following code sample uses sub-document operations to update the text attribute of a specified document.

Couchbase

MongoDB

Update Multiple Documents

In addition to primary access via key/value APIs, you can also execute N1QL queries via N1QL APIs. N1QL is a declarative language for querying, transforming, and manipulating JSON data – think SQL for JSON.

The following code sample executes a N1QL query to update the name and email attributes for all comment documents where name is Anat Chase. This query uses the idx1 index created above.

Couchbase

MongoDB

Update or Insert a Document

Use the Bucket.upsert() method to create a new document or update an existing document. If a document with the specified ID does not exist, upsert() will create a new document. If a document with the specified ID exists, upsert() will update the existing document. The following code sample updates an existing document in the sample_mflix bucket.

Couchbase

MongoDB

Delete a Document

Use the Bucket.remove() method to delete a full document with the specified ID. The following code sample deletes an existing document from the sample_mflix bucket.

Couchbase

MongoDB

Delete Multiple Documents

You can delete documents with N1QL queries. The following code sample executes a N1QL query to delete multiple documents from the sample_mflix bucket. All comment documents where the name is Anita Chase will be deleted. This query uses the idx1 index created above.

Couchbase

MongoDB

Data Access with N1QL

N1QL can also be used to perform more complicated secondary lookups of data. The following code sample executes a synchronous N1QL query to select the title, year, and imdb.rating from all movie documents where the year is between 1970 and 1979, ordered by the imdb.rating. The query uses the idx2 index created above.

Similar to batching key/value operations, asynchronous querying can be used for better performance.

Couchbase

MongoDB

What’s Next

Explore the other capabilities of the Couchbase SDK including Analytics and Full Text Search.

Take advantage of our free, online training available at https://learn.couchbase.com to learn more about Couchbase.

For detailed information on the architectural advantages of the Couchbase Data Platform over MongoDB see this document: Couchbase: Better Than MongoDB In Every Way.

Learn why other enterprises choose Couchbase over MongoDB:

DIRECTV

Viber

Nuance

Staples

Posted by Douglas Bonser, Principal Solution Engineer, Couchbase

Douglas Bonser is a Senior Solutions Engineer at Couchbase and has been working in IT and technology since 1991. He is based in the Dallas/Ft. Worth area.

Leave a reply