MongoDB Tutorial: Definition, Advantages, Terminology, Installation, Examples

MongoDB Tutorial: Definition, Advantages, Terminology, Installation, Examples

    1 8382
    mongodb tutorial


    What is Mongodb?

    Mongodb is an open-source or free document based nosql database system. Unlike a traditional relational database which stores data in tables, mongodb stores structured data as JSON-like documents, making the integration of data for certain types of applications really easier and faster.

    What is a nosql database?
    A non relational horizontally scalable, schema-free, easily replicable database is called nosql database.

    What is a document based database?
    A document based database like mongodb allows you to store documents, which are collections of keys and values of various types (array, integer, string, etc..) as well as child documents. Documents (objects) map nicely to any programming language data types and also embedded documents and arrays will reduce the need for joins.

    What are you going to learn?

    • Advantages of mongodb
    • Basic Terminology
    • Installing mongodb on mac, windows and linux
    • Mongodb Commands
    • Mongodb Examples

    Advantages of mongoDB

    • Mongodb keeps data together in documents, queries can be much faster Than in a relational database where related data is separated into multiple tables and then needs to be joined later.
    • Mongodb stores data in JSON documents which will be serialized to BSON format (binary json).
    • MongoDB provides features of a traditional RDBMS like secondary indexes, sorting, dynamic queries, rich updates, upserts (update if available, insert if not). This provides the functionality that you are used to in an RDBMS, and with the flexibility and scaling capability that a non-relational model allows.
    • MongoDB also makes it easy to scale out your database. Auto-Sharding helps you to scale your cluster linearly by adding more systems. It is possible to increase the capacity without any downtime, which is very very important on the web when load can increase suddenly and bringing down the website for extended maintenance.
    • Mongodb database replication methods helps faster replication, automatic failover and backup
    • Mongodb uses sharding techniques to distributes a single logical database system across a cluster of machines.
    • Mongodb’s map-reduce operations can handle very complex aggregation tasks. You can utilize map-reduce operations using the mapReduce command and also the mongo shell db.collection.mapReduce() wrapper method.
    • Mongodb’s GridFS is a specification for storing and retrieving files that exceed BSON document size limit of 16MB. Instead of storing a file in a single document, GridFS divides a file into parts, or chunks, and stores each of those chunks as a separate document.

    Companies depending on Mongodb

    Craigslist, foursquare, MTV Networks, EA,, Disney, IGN Entertainment, Intuit, Firebase, MongoHQ, SAP, SourceForge, Business Insider, Forbes, the new york times, chicago tribune, CNN turk,,, wordnik, CustomInk and 100’s of other top companies use mongodb.

    Basic Terminology

    Collection: A collection in mongodb is same as a table in mysql or oracle.
    Document: A document in mongodb is same as a row or record in any rdbms.
    JSON: Javascript Object Notation is a data-interchange format. It is easy for humans to read/write and also for machines to parse/generate.
    Key/value pair: ex: “name” : “nosql” :Here “name” is the key and “nosql” is the value.
    The default port number for mongodb is 27017.
    Every document in mongodb will have an unique _id.

    Installing Mongodb

    Install mongodb on windows

    • Visit>downloads->download the latest version based on your os architecture(32-bit or 64-bit).
    • Extract it to root (C:> drive on windows) and rename the folder to mongodb
    • Create a folder named “data” in C: drive with a sub folder named “db” (C:datadb).This is where mongodb store all its files.
      Now you can start mongodb server.
    • C:mongodbbinmongod.exe
      Open a new terminal and connect to mongodb server using
    • C:mongodbbinmongo.exe

    Install mongodb on Mac

    • brew install mongodb
    • use the command ‘mongod’ to start the server.
    • use the command ‘mongo’ to connect to the mongodb server.

    Install mongodb on Linux

    • Use /opt or /usr/local/bin.
    • sudo -i
    • sudo apt-get install mongodb
      now the mongodb installation is done.
      now download mongodb module for node.js (to connect and use mongodb)
    • npm install mongodb
      the installation is done.
      mongodb default storage location is
    • mkdir -p /data/db
    • chown mongodb /data/db
      To start the server
    • mongod –config /etc/mongod.conf (default file location is assumed at /etc/mongod.conf).
      to connect to the mongodb server
    • ./bin/mongo

    Mongodb commands

    show dbs: Displays the list of available databases.
    show collections: Shows the list of available collections
    use <database_name>: To enter into a database.


    If an existing document in the collection has the same _id, save() updates that document or it will insert the document.
    If you do not specify a document with an _id field to save(), performs an insert() operation.


    find() method is same as select statement in SQL.




    For larger collection we can use drop() method.

    Mongodb Examples

    Goto->your root directory->c:mongobin>mongod :This command will start the mongodb server.
    Open a new terminal(cmd prompt)->c:mongobin>mongo :This command will start the mongodb shell to execute your queries.
    Leta create a database first->use mongodb1 :This command will enter the database named ‘mongodb1′ if it exists, else it will create a database named ‘mongidb1′ and then will enter the database. syntax: use <db_name>

    Try this: Mongodb example1

    We will insert some data into the newly created database using insert() method.
    Syntax: db.collection_name.insert()
    Try: Copy the following code into your shell and press enter.
    name: { first: 'Sylvester', last: 'Stallone' },
    birth: new Date('July 06, 1946'),
    contribs: [ 'Rocky', 'Rambo', 'Assassins' ],
    awards: [
    award: 'Lifetime Achievement Award',
    year: 1997,
    award: 'Golden Apple',
    year: 1997,
    award: 'Honorary Cesar',
    year: 1992,
    name: { first: 'Clint', last: 'Eastwood' },
    birth: new Date('May 31, 1930'),
    contribs: [ 'Gran Torino', 'Million Dollar Baby', 'Escape from Alcatraz' ],
    awards: [
    award: 'AFI Award',
    year: 2012,
    movie: 'J. Edgar'
    award: 'Oscar',
    year: 2005,
    movie: 'Million Dollar Baby'
    award: 'Lifetime Achievement Award',
    year: 1996,
    name: { first: 'Leonardo', last: 'DiCaprio' },
    birth: new Date('November 11, 1974'),
    contribs: [ 'Inception', 'Shutter Island', 'Blood Diamond' ],
    awards: [
    award: 'Austin Film Critics Award',
    year: 2007,
    movie: 'The Departed'
    award: 'Silver Berlin Bear',
    year: 1997,
    movie: 'Romeo + Juliet'
    award: 'Golden Globe',
    year: 2005,
    movie: 'The Aviator'

    A Collection named ‘bio’ will be created and the data is inserted into that collection.
    We have inserted 3 documents, each document contains name, date of birth, contributions, awards information of a celebrity.
    A collection in Mongodb is same as table in SQL.

    Now to retrieve the inserted data you will use find() method().
    This Method will retrieve all the documents from the collection named ‘bio’.

    Try this: Mongodb example2

    Try: { “name.first”: “Sylvester” } )
    This method will retrieve all the documents having the first name as ‘Sylvester’.
    Here we have used .(dot) operator to query the sub documents.
    db.bios.findOne({ “name.last”: “Eastwood” } )
    The findOne() method will retrieve the first document having the last name as ‘Eastwood’.
    Try: { “awards.award”: “Lifetime Achievement Award” } ).limit(1)
    This command will limit the data retrieval to only one document. Remove the limit() method and the query will return 2 documents.
    Try: { “name.first”: “Sylvester” },{$set: {“name.last”:”Gardenzio Stallone”}} )
    The update() method using $set operator updated the existing document’s lastname from ‘Stallone’ to ‘Gardenzio Stallone’.
    Now try: { “name.first”: “Sylvester” } )
    Try: { ‘name.first’ : “Leonardo” } )
    The remove() method will delete the entire document which is having the first name as ‘Leonardo’.
    Now query:

    Now you know how to perform the CRUD operations using Mongodb.