MongoDB Tutorial – Introduction

Introduction to mongodb

MongoDB is a document database. The data model, queries and data stored as JSON (Javascript Object Notation). For me, the big differences between MongoDB and relational database model are flexibility, performance and scalability. Flexibility means that we can store various data structure in one single records, so we don’t have to joining multiple table as well as in relational database model or storing multiple values into single table. But it doesn’t mean that we can’t multiple join or transactions to individual document.

Because MongoDB store document as flexible structure, it’s easier to distribute or share data across multiple servers. That’s scalability feature of MongoDB. We have wide variety options deploying database. From many inexpensive commodity server to larger server.

Instead of storing data in rows and columns as a relational database does, MongoDB uses a document data model, and stores a binary form of JSON documents called BSON. Documents contain one or more fields, and each field contains a value of a specific data type, including arrays and binary data. Documents are stored in collections, and collections are stored in databases. It may be helpful to think of documents as roughly equivalent to rows in a relational database; fields as equivalent to columns; and collections as tables. There are no fixed schemas in MongoDB, so documents can vary in structure and can be adapted dynamically.

installation.

Installation procedure may vary depends on your environment. MongoDB runs on Windows, Linux, OSX and Solaris.You can follow installation step from official MongoDB.

 

Introduction to creating and reading documents.

MongoDB released with built it client interface called mongo-shell. This command line interface for administering or perform MongoDB operations such as CRUD. Open terminal, and type mongo and it will display typical output

There are a lot of commands available, just type  help to display lists of all commands. For example, if you want to list all databases stored type  show dbs and will display all databases available

So, here’s scenario, create database named Videos, store some documents into three different collections Movies, TV and Game. To do this, first type  use videos to use videos as default database that we use, MongoDB will automatically create it if it doesn’t exist. And let’s try to insert single document to movies collection.

acknowledge field is indicated that document we inserted succesfully, and insertedId is unique identifier to the document we inserted. MongoDB will automatically create _id field to hold insertedId as identifier. After insert some documents we can read all documents in collection by issuing command find() .

find() command takes a number of optional parameters, without passing parameters it’s just simply explore a collection. Remember when we insert single document by passing JSON document that match what structure our document will be store. In this case, we stored title of movie, year of movie and imdb. As well as find() command, we can pass specific parameter to match documents that we want to look for. For example, we want to find movie with title “American Sniper”, so we just pass it to find() command.

Note that i use pretty() command with find(), this command to make document easier to read. just try different parameters passed to find() to read specific document such as year or imdb.

See you on part 2.

 

 

abhiemanyu

 

One thought on “MongoDB Tutorial – Introduction

Leave a Reply

Your email address will not be published. Required fields are marked *