Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(U7DA) Introduction The database design process for noSQL databases is different

ID: 3755379 • Letter: #

Question

(U7DA)

Introduction

The database design process for noSQL databases is different from one for relational (SQL) databases.

As we learned noSQL database does not require to have a predefined structure, except of creating a list of databases and the list of collections. Some of the noSQL databases (like MongoDB) allow to define certain rules that will define what should be the structure of the acceptable documents for each collection.

As you may recall, when you need to store multiple data points in a relational database you use multiple records (rows) where each record and then values to describe properties of that record will be placed in the columns. Unlike relational databases, noSQL databases allow two ways of storing multiple values (lists):

By creating multiple documents (one per record);

By creating lists within a single document.

The choice between one or another is based on the potential size of the list, size of the records, and the most common operations the database will need to process. The rule of thumb will be if the list changes regularly and each item in the list or complex (an object itself), not very well connected to the other items, or the list is very long, then use one document per record approach. Note, that the size of the document may also be limited (e.g. 15MB for a MongoDB document).

If the list is small, it has with small records, and unlikely change often, then you can make it as a part of one document.

For example, if you have a database of the business transactions, and there could be millions of those transactions recorded per minute, create a new document for each of them with me more efficient choice. In contrary, when you store information about degrees that an employee earned, which will be updated rarely, all degrees can be combined into a list in one employee profile document.

MongoDB also allows to use relational database – like relations, by using a document id as a value in another document.

Business Case

A car-sharing company decided to use a MongoDB noSQL database to store information about their primary operations, which include recording of all rides, real time car locations, and users. A ride defined as combination of pick-up location, drop-off locations (if the car has been returned), with the associated timestamps of the those events.

Directions

In this assignment, you will propose a database structure for the given business case. It should include the list of collections, and one sample document (in JSON format) for each collection to demonstrate the document structure.

Put the list of the collections with the comments on their role along with the sample documents in JSON format.

Explanation / Answer

NoSQL databases have emerged tremendously in the last few years owing to their less constrained structure, scalable schema design, and faster access compared to traditional relational databases (RDBMS/SQL). MongoDB is an open source document-oriented NoSQL database which stores data in the form of JSON-like objects. It has emerged as one of the leading databases due to its dynamic schema, high scalability, optimal query performance, faster indexing and an active user community.

If you are coming from an RDBMS/SQL background, understanding NoSQL and MongoDB concepts can be bit difficult while starting because both the technologies have very different manner of data representation. This article will drive you to understand how the RDBMS/SQL domain, its functionalities, terms and query language map to MongoDB database. By mapping, I mean that if we have a concept in RDBMS/SQL, we will see what its equivalent concept in MongoDB is.

We will start with mapping the basic relational concepts like table, row, column, etc and move to discuss indexing and joins. We will then look over the SQL queries and discuss their corresponding MongoDB database queries. The article assumes that you are aware of the basic relational database concepts and SQL, because throughout the article more stress will be laid on understanding how these concepts map in MongoDB. Let's begin.

Mapping Tables, Rows and Columns

Each database in MongoDB consists of collections which are equivalent to an RDBMS database consisting of SQL tables. Each collection stores data in the form of documents which is equivalent to tables storing data in rows. While a row stores data in its set of columns, a document has a JSON-like structure (known as BSON in MongoDB). Lastly, the way we have rows in an SQL row, we have fields in MongoDB. Following is an example of a document (read row) having some fields (read columns) storing user data:

1

2

3

4

5

6

7

{

"_id": ObjectId("5146bb52d8524270060001f3"),

"age": 25,

"city": "Los Angeles",

"email": "mark@abc.com",

"user_name": "Mark Hanks"

}

This document is equivalent to a single row in RDBMS. A collection consists of many such documents just as a table consists of many rows. Note that each document in a collection has a unique _id field, which is a 12-byte field that serves as a primary key for the documents. The field is auto generated on creation of the document and is used for uniquely identifying each document.

To understand the mappings better, let us take an example of an SQL table users and its corresponding structure in MongoDB. As shown in Fig 1, each row in the SQL table transforms to a document and each column to a field in MongoDB.

1

2

3

4

5

6

7

{

"_id": ObjectId("5146bb52d8524270060001f3"),

"age": 25,

"city": "Los Angeles",

"email": "mark@abc.com",

"user_name": "Mark Hanks"

}