Notes on JSON Modeling for Document Databases

Notes on JSON Modeling for Document Databases

·

5 min read

Notes from a presentation by https://twitter.com/mgroves . Replay available https://www.youtube.com/channel/UC5Cjdv38sS_yNEue-QUAF7g

Agenda:

  • Why NoSQL?
  • JSON Data Modeling
  • Accessing Data
  • Migrating Data
  • Summary / Q&A

Why NoSQL

"Change is inevitable" and relational databases don't handle change as well. NoSQL databases scale much easier

Types of NoSQL Databases:

image.png

Document DB

  • Get, Set, Replace, Delete by key(s) Simplest type of a key-value store Scalability comes in the form of multiple nodes in a cluster

No inherent relationship between data. Provides flexibility

Performance

Use Cases: Caching, session, user profile, content management Other users: gaming, advertising, travel booking, loyalty programs, fraud monitoring, social media, finance, customer 360, IoT, communication

JSON Data Modeling

Properties of Real-World Data: example: customer

  • Name
  • DoB
  • Billing
  • Connections
  • Purchases

image.png

The old way of relational databases: image.png As schemas get more complex, it gets harder to add tables and columns.

RDBMS vs DocumentDB:

Standard Customer table: image.png

Introduce another table:

image.png

RDBMS would require a "Join" at this point, but with the document model, it's still just one piece document

Adding more rows is easy by adding items to the array

(quotes are not optional for valid JSON)

Creating the connections array refers to another document with a documentkey ID. image.png

The full model:

image.png

image.png

Ways to control documents: You can version documents with different ways to break out the model Does not require a change to pk or fk.

image.png

Can also reversion with a web application. Get data from old document and save it to the new (user initiated)

Tools to help with modeling

image.png

jsoneditoronline.org is the only free one.

Accessing Data

SQL is accessed via SQL Query. NoSQL has many many options to access. -

Example using C#:

image.png

Recommendations for keys:

  • Natural Keys (something that has a value that makes sense, but that won't change)
  • Human Readable
  • Deterministic
  • Semantic

Deterministic means that a "march" through the data makes sense.

image.png

Example: Find author, pull up author's blogs, go to blog posts, pull blog posts comments

Relationships:

image.png

Subdocument Access (API)

Update via subdocument api, can speed up updates without accessing the whole document

Other ways to access the data

image.png

N1QL (SQL for Document DB) Example:

image.png

Search with index and use suggestions to improve performance.

Full Text Search - find all the results that have the word you're looking for

image.png

Good for lots of things like inverted index, geographical

image.png

Use the right concept to access the data, and model the data accordingly.

Migrating

image.png

Tools:

image.png

image.png

Do it with a simple CSV output and import. Once data is in place, start using or transforming that data into a future state.

image.png

Align your data with the ways you want to access it.

CouchBase ships with samples.

DEMO

Demo showed creating a bucket and running a C# program to convert the data from SQL Server to Couchbase. Bucket contains Scopes, Scopes contain collections, collection contains documents

Take Away

Couchbase Download CouchBase Influitive

Link to github repo of demo