NoSQL Databases

NoSQL Databases and designing a schema-less Data Model

MongoDB Docs & Tutorial

My background is deeply rooted in the more traditional relational database model in which we generally build our databases using a database server like MySQL or PostgreSQL. Next, we add tables which consist of columns and rows. Not unlike and Excel worksbook. Each table contains a Primary key and related tables can contain both a Primary Key and a Foreign key. We can then easily create the necessary relationships between our tables that contain related data. This is the most basic principles of RDBMS.

Those key relationships help use group our data into a logical object model that we can use to write our business applications. This database model is very easy to understand and database diagrams are easily drawn to express these relationships visually.

Over the past several years, many new database engines have emerged. NoSQL databases refer to all document object model databases that store their data as documents. Generally, these are JSON document objects.

One of the most popular of these NoSQL databases is MongoDB. MongoDB stores it's data as a binary representation of the JSON object. That makes MongoDB super fast and highly scalable.

In RDBMS, data is stored in tables. In MongoDB, data is stored in collections as key-value pairs.

And since there is no strict database schema to follow, databases and collections are created with ease. Let's sample a bit of Python in our example.

get_collections.py

import pymongo  
connection = pymongo.MongoClient('localhost', 27017)

# if the database does not exist, MongoDB creates it automatically.
db = connection.test   #this sets the database to test

# if the collection does not exist, MongoDB will create it automatically.
names = db.names

# let's do a quick query while we are here
names = db.names.find()

for name in names:  
  if name['name'] == 'Craig Derington':
    print(name)

#drop names
names = db.names.drop()  

This makes storing data incredibly simple. Because our data is flexible, our business applications can be just as flexible when our business requirements change and data is added, modified, reorganized or removed from the model.

To download MongoDB and begin interacting with the Mongo shell, click here

Once you have MongoDB installed, set up a batch file to launch both the Mongo engine and the Mongo shell simultaneously.

Create a blank text file, save it as mongo.bat on your desktop so you can find it quickly.

Next, create folder on your hard drive. I recommend C:\mongodb\data for ease of use.

In your text file, enter...

start C:\progra~1\mongodb\server\3.0\bin\mongod.exe --dbpath C:\mongodb\data  
start C:\progra~1\mongodb\server\3.0\bin\mongo  

This will start MongoD, the database engine that powers MongoDB and the interactive shell, which allows you to create databases and collections, read and write data to the Mongo database.

Now that your Mongo shell is open, you should have a command prompt that looks like so...

MongoDB shell  
connecting to: test  
>

This is where we begin to talk to our Mongo DB.

connecting to: test  
> show dbs
admin  
local  
test  
reddit  
m101  
> use test
switched to db test  
> show collections
accounts  
arrays  
names  
people  
scores  
things  
users  
system.indexes  
> db.arrays.find();
{ '_id' : 0, 'a' : [2, 4, 7, 8, 9]}
> db.arrays.insert({ '_id' : 1, 'b' : [10, 11, 12, 13, 14, 15]});
WriteResult({ 'nInserted' : 1 })  
> db.arrays.find().pretty();
{ '_id' : 0, 'a' : [2, 4, 7, 8, 9]}
{ '_id' : 1, 'a' : [10, 11, 12, 13, 14, 15]}

Being able to store different sets of data fields for each document in the collection makes MongoDB an incredible powerful database engine. I can store one set of data for one document and store a completely different set of data for another document.

connecting to: test  
> db.names.insert({ 'name' : 'Craig Derington', 'address' : '1234 Main Street', 'fav_color' : 'blue' });
WriteResult({ 'nInserted' : 1 })  
> db.names.insert({ 'name' : 'James Watson', 'email' : 'james@watson.org' });
WriteResult({ 'nInserted' : 1 })  
db.names.find().pretty();  
{
  '_id' : ObjectId('561c0101..')
  'name' : 'Craig Derington',
  'address' : '1234 Main Street',
  'email' : 'craig@...'
  'role' : 'admin'
}
{
  '_id' : ObjectID('561c0102..')
  'name' : 'James Watson',
  'email' : 'james@watson.org'
}

Now, in the names collection, I am storing the given name and surname for each document, but notice how address, email and role are stored in the first document ID. Only name and email is stored for the second document ID.

This is super useful for storing, reading, updating, and destroying objects in your database.

For application developers, it means designing a data model that is fast, flexible, easily deployable across multiple shards; and changes will not require any database modifications, like MySQL or Microsoft's SQL Server.

Changes to the database model are immediately invoked in your collection when the first object is instantiated.

connecting to: test  
> db.names.update({ 'name' : 'James Watson', { 'name' : 'James Watson', 'email' : 'james@watson.com', 'role' : 'administrator' }, { upsert : true }});
WriteResult({  
  'nMatched' : 0,
  'nUpserted' : 1,
  'nModified' : 0,
  '_id' : ObjectId('561c0922...')
})

Did you see what just happened there? MongoDB allowed me to simply just dynamically add an additional field to my name model. I just added the role field to James Watson's profile with an Upsert = true statement, a unique feature of NoSQL databases.

MongoDB: Data Models for NoSQL Databases

MongoDB Data Models

As I mentioned earlier, the data model in MongoDB is not set in concrete and can be changed on the fly. It is completely flexible and the MongoDB engine does not enforce document structure.

When considering designing a schema-less data model for MongoDB, ensure you cover the following topics.

  • Document Structure
  • References
  • Embedded Data
  • Atomicity
  • Document Growth
  • Data Performance
MongoDB Shell commands

MongoDB Database Command

Most used:

Aggregation Operations:  
count  
distinct  
group  
mapReduce  
Query and Write Operations:  
insert  
update  
delete  
findAndModify  
User Management Operations:  
createRole  
updateRole  
dropRole  
rolesInfo  
dropAllRolesFromDatabase  
Instance Administration Operations:  
copydb  
dropDatabase  
listCollections  
drop  
create  
clone  
cloneCollections  
listIndexes  
dropIndexes  
clean  
shutdown  

Craig Derington

Veteran full stack web dev focused on deploying high-performance, responsive, modern web applications using Python, NodeJS, Django, Flask, MongoDB and MySQL.

comments powered by Disqus