MongoDB Review / Summary 4.0

MongoDB Review as of Version 4.0 August 2018

MongoDB Overview

MongoDB is referred to as a “Document Database” as it supplies a very flexible JSON document like structure.
Very generally, it’s major trends and features are:

  • Growing in popularity – released in 2009 – current version as of 2018 is MongoDB 4.0
  • Collections in MongoDB are somewhat analogous to tables in an RDBMS, collections are made up of documents (rows)
  • Documents can more simplistically model real world objects than a set of RDBMS tables  operations by nesting objects within a document.  Making a document object completely self-contained – versus RDBMS database that normalize an object across tables
  • Documents (similar to records in an RDBMS table) but different in the sense that each document in a collection can have different attributes and can contain nested attributes
  • CRUD operations are implemented by providing an easy to use function/API based  interface for both input and output – not SQL like
  • Multi-Document ACID Transactions – only and just now added to version 4.0+ – not implemented with sharded clusters until 4.2
  • Specialized indexing is supported to optimize distribution of data across a sharded cluster (hash index), and index types that support special types of searches (geo, text based, and a unique document index always present named _ID) otherwise quite similar to typical RDBMS indexing
  • Python, and javascript / node.js are widely used with MongoDB – drivers supplied and for other languages
  • MongoDB replicas support failover capabilities
  • Clustering with shards – provides horizontal scaling
  • A JavaScript client / CLI “mongo” that  is widely used and installed with MongoDB can be used local or remote
  • MongoDB runs on Windows and Linux on premise
  • MongoDB can be installed on-prem and in the cloud
  • MongoDB’s cloud offering is named Atlas – and is provision-able in at least Microsoft Azure, Google’s GCP and Amazon’s AWS clouds – released first in AWS June 2016
  • Mongo University provides training (many free) and a Developer and DBA certification
  • A US public corporation currently listed on the NASDAQ – just listed 2017 not yet profitable
  • MongoDB default settings are not secure as of current release 4.0
  • MongoDB Enterprise Advanced provides numerous advanced features and additional components to ease use and meet governance requirements at a price – TCO is estimated to be 30% (70% savings) of an Oracle Enterprise database solution with similar features – strictly from a hardware and software licensing perspective
  • No foreign keys to enforce data integrity – more research necessary

MongoDB Object Structures

Database Structure

MongoDB is schema-less – collections (table like structures) are simply created within a database by inserting data into them.

Table Structure

The thing that is closest to a table in MongoDB is called a collection.  No relationships between tables are or can be created.
As an example screenshot – explanation coming up:

There is no “create collection” statement. You simply add documents to a collection.
If the collection does not exist, it is created.
For example to create one of the rows / documents above and the collection itself if it did not exist you could do the following:

db.Customer.insertOne(
{ CustomerID : 99,
CustomerName : "Nicole Wallace",
OrderID : 333
} )

Record / Tuple Structure

Records are called “Documents”.  They have dynamic structures, that is within a Collection each document can have differing fields.

Fields

Fields are made up of:

Key – e.g CustomerName

Value – e.g. “Nicole Wallace”

Unlike relational databases there are no column names.  For a given document, the number, types, and meaning of the fields in a document (a record to me yes I come from a relational background) can and may very well vary.

Binary JSON (BSON)

MongoDB represents JSON documents in binary-encoded format called BSON behind the scenes. BSON extends the JSON model to provide additional data types, ordered fields, and to be efficient for encoding and decoding within different languages.

Mongo Indexing

MongoDB is capable of indexing both primary and secondary and it is highly suggested.  The unique identifier to a collection is the built-in attribute “_ID”.  Primary and Secondary Key / Indices are created at your discretion.
Specialized indexing is supported to optimize distribution of data across a sharded cluster (hash index), and index types that support many special types of searches (geo, text based and several others).  From an indexing perspective the product differentiates itself with a multitude of potentially useful indexing approaches that can be used to optimize access for a variety of application types. Otherwise basic MongoDB indexing is quite similar to typical RDBMS indexing.
For more information – see the MongoDB indexing documentation below:
https://docs.mongodb.com/manual/indexes/?utm_campaign=T5_V2_MongoDB_Enterprise_Edition_Download_Email3&utm_medium=email&utm_source=Eloqua

Getting Started / Installation

MongoDB Architecture Guide
MongoDB Installation on RHEL / CentOS
Installation on Linux requires root.

echo $PATH

# your path should include the mongodb install directory – which if you did an RPM install is most likely:

/usr/bin

Once your data base is up – interact with a simple CLI

# Here is a quick link to a video with tutorial on using the mongo javascript shell
https://www.linkedin.com/learning/learning-mongodb/explore-the-mongo-shell
# “mongo” is a very simple javascript based command line interface to access mongodb databases
# path is normal /usr/bin/mongo with default RPM installs 
# here is the documentation for it (I use this locally just by typing “mongo”):
https://docs.mongodb.com/manual/mongo/
# Per the MongoDB documentation: To Create a Database
# If a database does not exist use the “use db” command, then MongoDB creates the database when you first store data for that database.
# So it is a two step operation to create a DB
# As such, you can switch to a non-existent database and perform the following operation in the mongo shell:

[root@rhel74 bin]# mongo
use myNewDB
db.Customer.insertOne( { x: 1 } )
db
myNewDB
MongoDB Enterprise > show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
# test db has zero elements so it has not been created yet - it will be if you create objects
# but to create a db named myNewDB...
MongoDB Enterprise > use myNewDB
switched to db myNewDB
MongoDB Enterprise > db.Customer.insertOne( { x: 1 } )
{
"acknowledged" : true,
"insertedId" : ObjectId("5b7416055a97d3d80f9347d8")
}MongoDB Enterprise > help
# gives you lots of help... like the following and much more
show dbs #show database names
show collections #show collections in current database
show users #show users in current database
show profile #show most recent system.profile entries with time >= 1ms
MongoDB Enterprise > show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
myNewDB 0.000GB
quit()
[root@rhel74 bin]#

Users And Security

Adding an Admin User

use admin
db.createUser(
{
user: "myUserAdmin",
pwd: "abc123",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)

Remote Connections To MongoDB

# Make sure the server that MongoDB is installed on, opens the MongoDB port (default 27017) thru the firewall – or disable the firewall and then modify the MongoDB configuration file – default file and location on RHEL is /etc/mongod.conf as shown below.
# RHEL 7 command to open firewalld port – using sudo (or root):

firewall-cmd –zone=public –add-port=27017/tcp –permanent

# modify the /etc/mongod.conf file to allow remote connections
# alter 127.0.0.1 to 0.0.0.0 – careful open to the world unless you are behind another firewall – which is usually the  case – mod to:

bindIp: 0.0.0.0

# Add the following lines beneath # security

security:
authorization: 'enabled'

# mongo syntax for remote connections

mongo mongodb://username:pw@hostname:27017/admin
# or if you are just using the "mongo" CLI
mongo --host hostNameOrIP --port 27017 -u "username" -p "pw" --authenticationDatabase "admin"
note: port 27107 is the default yours may differ and "admin" is the name of a default DB yours may differ there too
note2: if you copy the connection string of the form mongodb://username:pw@hostname:27017/admin into your
paste buffer Mongo Compass can use that to build a connect string

More on security

Opening ports and enabling security – more to come – here is something brief:
https://ianlondon.github.io/blog/mongodb-auth/
And here is something more thorough that includes SSL setup
https://www.cyberciti.biz/faq/how-to-secure-mongodb-nosql-production-database/

MongoDB Tutorials And Traing

MongoDB the company provides several training courses – mostly free – on MongoDB – after creating an account you can sign up for them at:
https://university.mongodb.com/
# Additionally, here is a blog that references several short tutorials on MongoDB
https://www.guru99.com/mongodb-tutorials.html

MongoDB Replicas

Good MongoDB replica set example from linode
Very simple replica setup insecure though from dzone
Setup MongoDB Replica Set in 4 steps

Clustering MongoDB with Shards

Overview and Setup Of A MongoDB Cluster

MongoDB Performance

  • Profiling
//command will turn slow query logging
//queries longer than 100
db.setProfilingLevel(1, 100);
  • Indexing – inner and outer and much more
  • Sharding
  • Replicas

https://www.codemag.com/article/1309051/To-MongoDB-or-Not-to-MongoDB

MongoDB Data Integrity

A massively important subject to address related to a free format “documents” type database like this… to be addressed

MongoDB Locking

To be written

MongoDB – On Premise Versus Atlas In The Cloud

Coming

The default GUI into the MongoDB – Compass

# Mongo Compass is a GUI into MongoDB – it is nice but – optional
To Install Mongo Compass GUI on RHEL/CentOS 7 Click Here
# start compass on RHEL 7 – or there is a community edition issue the Linux command, o see what is installed type:

[root@rhel74 bin]# ls -1 /usr/bin/mongo*
/usr/bin/mongo
/usr/bin/mongoexport
/usr/bin/mongos
/usr/bin/mongod
/usr/bin/mongofiles
/usr/bin/mongostat
/usr/bin/mongodb-compass-community
/usr/bin/mongoimport
/usr/bin/mongotop
/usr/bin/mongodecrypt
/usr/bin/mongoldap
/usr/bin/mongodump
/usr/bin/mongorestore
[root@rhel74 bin]# mongodb-compass-community

MongoDB CRUD Statements Summary – See MongoDB Documentation  Links For Further Details

https://docs.mongodb.com/manual/crud/
Note: the above MongoDB CRUD doc refers to “example” docs within it, that have excellent examples – please refer to these individual docs.  A summary of CRUD operations is posted below.
Mongo CRUD operations bear no resemblance to SQL – the following is a summary copied from MongoDB Docs
MongoDB provides the following methods to insert documents into a collection:
db.collection.insertOne() New in version 3.2
db.collection.insertMany() New in version 3.2
In MongoDB, insert operations target a single collection. All write operations in MongoDB are atomic on the level of a single document.
Read Operations
db.collection.find()
You can specify query filters or criteria that identify the documents to return with variations of the function above.
Update operations modify existing documents in a collection. MongoDB provides the following methods to update documents of a collection:
db.collection.updateOne() New in version 3.2
db.collection.updateMany() New in version 3.2
db.collection.replaceOne() New in version 3.2
In MongoDB, update operations target a single collection. All write operations in MongoDB are atomic on the level of a single document – see MongoDB docs for more info.
You can specify criteria, or filters, that identify the documents to update. These filters use the same syntax as read operations.
The components of a MongoDB updateMany operation.
For examples, see MongoDB Update Documents.
Delete operations remove documents from a collection. MongoDB provides the following methods to delete documents of a collection:
db.collection.deleteOne() New in version 3.2
db.collection.deleteMany() New in version 3.2

MongoDB Enterprise Advanced

MongoDB Enterprise Advanced provides additional features and components over and above Mongo Enterprise  at an additional cost such as:

Ops Manager or Cloud Manager Premium ✔
Red Hat Identity Management Certification ✔
Kerberos & LDAP Authentication; LDAP Authorization ✔
Auditing ✔
SNMP Support ✔
Encrypted Storage Engine ✔
In-Memory Storage Engine ✔
MongoDB Compass ✔
Advanced Analytics ✔
Platform Certifications: Windows, RedHat/CentOS, Ubuntu, Amazon Linux, Linux on z Systems, LinuxOne,
Linux on Power Systems ✔
Private, On-Demand Training ✔
Support SLA 1 hour
Support Availability 24 x 365
Emergency Patches ✔
Commercial License, Warranty, and Indemnification ✔

http://info-mongodb-com.s3.amazonaws.com/MongoDB_Enterprise_Advanced_Datasheet.pdf

MongoDB Ops Manager

GUI based tool that provides additional features such as:

Deployment
Management
Upgrades
Scaling
Query Optimization
Point In Time Backups
Performance Alerts

https://www.mongodb.com/products/ops-manager

MongoDB Backup and Restores

MongoDB provides at least 3 options for on premise databases.

Ops Manager (per MongoDB docs.) included with Enterprise Advanced License

With Ops Manager, MongoDB subscribers can install and run the same core software that powers MongoDB Cloud Manager on their own infrastructure. Ops Manager is an on-premise solution that has similar functionality to MongoDB Cloud Manager and is available with Enterprise Advanced subscriptions.
For more information about Ops Manager, see the MongoDB Enterprise Advanced page and the Ops Manager Manual.

Back Up by Copying Underlying Data Files Back Up with Filesystem Snapshots (e.g. AWS EC2 EBS Snapshot)

Make sure you turn on oplog / logging before using file system snapshots.

Mongodump and mongorestore

Per MongoDB documentation: mongodump reads data from a MongoDB database and creates high fidelity BSON files which the mongorestore tool can use to populate a MongoDB database. mongodump and mongorestore are simple and efficient tools for backing up and restoring small MongoDB deployments, but are not ideal for capturing backups of larger systems.

MongoDB References

DB Engine Rankings (MongoDB currently 5th on this website anyway)

https://db-engines.com/en/ranking

MongoDB Enterprise Versus Community Edition

Enterprise edition has a licensing fee for production use.  And enhanced security, authentication/authorization, auditing, monitoring integration, backup, and support licensing features etc.

Release History and Support

Product was released 2009 V1.  Here is a link to support and release history.
https://www.mongodb.com/support-policy

MongoDB Documentation

https://docs.mongodb.com/

MongoDB Presentations

https://www.mongodb.com/presentations

Installing PyMongo For Python

Follow the instructions in the link below to install pip, wheel and setup tools.  The link below gives you a couple of options.
https://packaging.python.org/guides/installing-using-linux-tools/

# then
yum upgrade python-setuptools
yum install python-pip python-wheel
python -m pip install pymongo
More from LonzoDB on AWS

Leave a Comment

Scroll to Top