Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Learning Couchbase

You're reading from  Learning Couchbase

Product type Book
Published in Nov 2015
Publisher
ISBN-13 9781785288593
Pages 248 pages
Edition 1st Edition
Languages
Author (1):
Henry Potsangbam Henry Potsangbam
Profile icon Henry Potsangbam

Table of Contents (12) Chapters

Introduction to Couchbase The Couchbase Administration Interface Storing Documents in Couchbase Using Buckets Designing a Document for Couchbase Introducing Client SDK Retrieving Documents without Keys Using Views Understanding SQL-Like Queries – N1QL Full Text Search Using ElasticSearch Data Replication and Compaction Administration, Tuning, and Monitoring Case Study – An E-Commerce Application Index

Chapter 4. Designing a Document for Couchbase

The chapter will introduce the concepts of JSON, compare NoSQL with RDBMS, and explain how to manage relationships between various documents. It will also familiarize you with the document editor option so that you can create and edit a document using the web UI. We will try to understand the various ways of designing a document for a Couchbase cluster. Earlier, we specified that the developer doesn't need to worry about the schema. However, while developing any application, we do need some kind of schema modeling to provide a coherent schema and represent domain objects, though nothing as stringent as an RDBMS table structure.

Understanding JSON and non JSON data


Let's try to understand the representation of a document in Couchbase. A document in Couchbase can be represented by JSON as well as by non JSON binary data. We have already explored the JSON format in an earlier chapter, and now you might be wondering why I am trying to explain it further. When you design a document for storage and retrieval from Couchbase, you will be working extensively in the JSON format. Hence, understanding it in detail is very important when it comes to designing documents for performance and efficiency.

JSON is a text format that is completely language-independent and a lightweight data interchange format. It is built on two structures, as follows:

  • A collection of name and value pairs: Depending on the type of language, it can be realized as an object, record, dictionary, hash table, keyed list, or associative array

  • An ordered list of values: This is represented as an array, vector, or list

Its representation is quite similar to that...

Document versus RDBMS


Before you understand the various considerations that need to be kept in mind while designing a document for any document-based system, particularly a Couchbase system, let me explain what a document really is and how it differs from RDBMS.

Often during my training sessions, participants have asked me what a document is really all about. They are often confused about document files, such as .pdf or .doc file formats. This terminology doesn't represent a file as you perceive in software world. Here, "document" means representation of information as perceived in reality or in the physical world. In reality, when we represent information, we discuss or represent it as a single complete document without breaking it into multiple documents and linking them to each other. For example, when we represent an invoice document, there are different sections, such as invoice, address, and items. If you need to store a domain object in a document, it will be represented as whole information...

Document modeling


In order to bring agility to applications that change business processes frequently demanded by its business environment, being schemaless is a good feature. In this methodology, you don't need to be concerned about structures of data initially while designing the application. This means as a developer, you don't need to worry about structures of a database schema, such as tables, or splitting information into various tables; instead, you should focus on application requirement and satisfying business needs.

I still recollect various moments related to design domain objects/tables, which I went through when I was a developer, especially the time when I had just graduated from engineering college and was into developing applications for a corporate. Whenever I was a part of any application requirement discussion, I always had these questions at the back of my mind:

  • How does a domain object get stored in the database?

  • What will the table structures be?

  • How will I retrieve the...

Document relationships


In a document-based database system, such as Couchbase, you don't need to worry about altering tables when there are changes in the schema, as we usually have to do in RDBMS. All schemas of a document are driven by application code only. Whenever there is a need to change schema, we don't need to worry much, as schema of a document goes along with the document itself as metadata. We don't need to change any schema explicitly at the bucket level. We just need to think about the latency, update, and read pattern of documents in terms of dataflow while designing document. We will be more concerned about data flow than data storage. However, my experience leads me to feel, that if you can design a document as a standalone entity, that's the best thing.

We have seen two ways of designing documents: one that includes all related information in one document and others splitting information into multiple documents. If we have to choose the second option, there should be a way...

Using the document editor


Let's familiarize ourselves with the document editor provided in the Couchbase admin console. Let's create the document that was just discussed using the document editor. Make sure that you store the user and order documents as defined in the bucket, LearningCouchbase that we created earlier.

User

You can enter the User document using the document editor of the web console, as shown here:

The User document

Order

Just like User, enter the Order details as follows. Make sure that you select the LearningCouchbase bucket.

Order document

Now you know how to create a document using the document editor. Let me explain how you can update an attribute, say quantity, in the order document. Go to the LearningCouchbase bucket's document display list, as shown in the following screenshot. Key in its document ID, which is 2015051527, in the Lookup Id textbox, and click on the Lookup Id button.

Documents

Change the quantity from 6 to 8 and save it as follows:

The document editor - updating...

Summary


In this chapter, we took a brief overview of documents and how to model a document for a particular business requirement. We also discussed some important design considerations for maintaining relationships between various documents. In addition to that, we discussed the difference between documents and RDBMS. Finally, you learned to use the document editor and create and edit a document using the web console.

In the next chapter, you will understand how to connect to Couchbase using the Java API, and we will cover various operations that can be performed with it.

lock icon The rest of the chapter is locked
You have been reading a chapter from
Learning Couchbase
Published in: Nov 2015 Publisher: ISBN-13: 9781785288593
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}