MAY 08, 2015 11:51 AM
What is mongoDB
MongoDB is a document database. This is a different concept than a SQL database where most developers are familiar with. In mongoDB, databases are referred to as collections (you can host multiple collections on a mongoDB sever), and instead of tables there are documents. A document is a set of key-value pairs with a defined schemas. But what is amazing about mongoDB is the fact that document schemas are very dynamic and can change as your business needs change. This means that if starting today you need to add additional fields to newer records there is no need to do the same for the older ones without breaking your document.
Key-Value Pairs
Documents in mongoDB are a set of key-value pairs. If you are familiar with JSON then this is easy to grasp. Here is an example of a schema:
var CampaignsSchema = new Schema({
campaign_id : { type: Number, required: true }
, name : { type: String, required: true }
, description: String
, startdate: {type: Date, required: true}
, enddate: Date
, updatedate: {type: Date, required: true}
, campaigntype: Number
, exposurelimit: Number
, creatoruname: {type: String, required: true}
, status: {type: Number, required: true}
, comments: String
, brand_id: { type: Number, required: true }
, splashimage: String
, scanhistoryimage: String
, qrcodehistoryimage: String
, dashimage: String
, listimage: String
});
A schema is not necessary to create a document as mongoDB will create the it on the fly so long as the collection has been created. I use the schema above as an example of JSON and ODM (Object Data Mapping) used in Mongoose (a Node.js library).
Installing mongoDB
Installing and configuring mongoDB is quite straight forward. Instructions are found here:
http://docs.mongodb.org/manual/tutorial/install-mongodb-on-os-x/
After completing the steps outlined in the web page you should be able to start mongoDB.
Using mongoDB
Before you can start using mongoDB you’ll need to configure your server. Note that I downloaded mongoDB tar ball and proceeded to build it myself instead of using “brew”. This way I have more control over where it is installed. The following is a partial view of my mongodb.conf file:
# mongodb.conf
# Where to store the data.
# Note: if you run mongodb as a non-root user (recommended) you may
# need to create and set permissions for this directory manually,
# e.g., if the parent directory isn't mutable by the mongodb user.
dbpath=/data/db
#where to log
logpath=mongodb.log
logappend=true
fork = false
bind_ip = 127.0.0.1
nounixsocket = true
port = 27017
#quiet = true
# Disables write-ahead journaling
# nojournal = true
# Enables periodic logging of CPU utilization and I/O wait
#cpu = true
# Turn on/off security. Off is currently the default
#noauth = true
#auth = true
# Verbose logging output.
verbose = true
The manual installation comes with a configuration file. You will need to set your own parameters before starting your server.
And you start the server as follows:
> /path/to/mongoDB/installation/bin/mongod --config mongodb.conf
Next login to the server:
> mongo
MongoDB shell version: 2.6.4
connecting to: test
Server has startup warnings:
2015-05-08T11:23:44.050-0700 [initandlisten]
2015-05-08T11:23:44.050-0700 [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
I have already created a collection named luxitouch_tdb, but using “use” will also create a new collection if it does not exist already. This is how you can switch to your existing or create a new collection:
> use luxitouch_tdb
switched to db luxitouch_tdb
>
What Next
Next week I will set up my Node.js to use my mongoDB. It’s quite easy as you’ll see. If you have questions do let me know and I will be more than happy to answer your questions.