Getting started with MongoDB for postgreSQL devs

If you primarily work with postgreSQL (or any relational database systems) and have heard a lot of buzz regarding NoSQL databases and want to get started in one of those, here's a breif intro to get started on 'mongoDB' with one-one comparision with postgreSQL. It's one of the widely used NoSQL database (especially if you work with RESTful APIs).

There's a number of different classification of NoSQL databases: document-based, key-value based, etc.

mongoDB is a document based NoSQL database.

Creating a new DB

Creating a new database is just plain one command into the mongo shell. Type mongo on your terminal/bash shell to enter the mongo shell and use this one liner to create a new DB.

mongo_shell> use blogDb;
# --
template1=#> CREATE DATABASE blogDb;

Dropping a DB

Dropping an existing database from mongo shell is another one liner.

mongo_shell> db.dropDatabase()
# --
template1=#> DROP DATABASE blogDb;

Creating a new collection

'collections' can be analouged with what we have 'tables' in traditional relational databases.

mongo_shell> db.createCollection('posts')
# --
template1=#> CREATE TABLE posts (title VARCHAR(20), tag VARCHAR(20), permalink VARCHAR(20), content TEXT);

Dropping a collection

Dropping a 'collections' is just chaining drop() command on the name of the collection of the DB currently in use.

mongo_shell> db.posts.drop()
# --
template1=#> DROP TABLE posts;

CRUD operations


mongo_shell> db.posts.insert({ title: 'hello world', tag: 'dbms', permalink: 'db-mongo', content: 'start something with hello world!' })
# --
template1=#> INSERT INTO posts (title, tag, permalink, content) VALUES ('hello world', 'dbms', 'db-mongo', 'start something with hello world!');

READ all documents from a collection

mongo_shell> db.posts.find()
# --
template1=#> SELECT * FROM posts;

READ a single document from a collection

mongo_shell> db.posts.find({ permalink: 'db-mongo' })
# --
template1=#> SELECT * FROM posts WHERE permalink='db-mongo';

UPDATE a single document

mongo_shell> db.posts.update({"permalink": "db-mongo"}, {$set : {"title": "hello mongoDB", "tag": "database"}})
# --
template1=#> UPDATE posts SET title='hello mongoDB', tag='database' WHERE permalink='db-mongo';

Bulk UPDATE multiple documents

mongo_shell> db.posts.update({}, { $set : { "tag": "nosql"}}, { multi: true })
# --
template1=#> UPDATE posts SET tag='nosql';

DELETE a single document

mongo_shell> db.posts.remove({ "permalink": "db-mongo" })
# --
template1=#> DELETE FROM posts WHERE permalink='db-mongo';

Bulk DELETE multiple documents

mongo_shell> db.posts.remove({})
# --
template1=#> DELETE FROM posts;

Miscellaneous


Export - dump BSON formatted DB data into dump directory at current directory

mongo_shell> mongodump --db blogDb
mongo_shell> mongoexport --db blogDb --collection posts --out data/posts.json --jsonArray --pretty
mongo_shell> mongoexport --db blogDb --collection posts

Import - BSON exported, gzip compressed data to a new DB

mongo_shell> mongorestore --db db_to_create --gzip /path/of/dump
mongo_shell> mongoimport --db blogDb --collection posts --jsonArray posts.json