MongoDB Introduction

1
1250

What is MongoDB

MongoDB is Non-relational JSON document store. It is called non-relational because it does not support SQL queries as in case of relational databases, and it is JSON document store because the document it stores is structured in JSON format (BSON or Binary JSON to be more precise). The name is taken from word humongous. It is an open-source NoSQL document-oriented database. MongoDB does not support the concept of RDBMS joins and also purposefully does not support the concept of transactions across multiple collections.

Installation

At the time of writing this article I found installation guidance Here.

MongoDB shell

MongoDB comes with its own shell to interact with the database. MongoDB has a concept of collection to store data. A collection is associated with a database and you use ‘db.collectionName.operationCommand’ to interact with the data stored within the database, from Mongo shell. Remember that collectionName and operationCommand are placeholders in the above.

Step-By-Step guide to use Mongo shells

Following steps details out how to start Mongo shell and use mongo shell for CRUD operation.

1)    Check “Installation” section to download and install MongoDB
2)    Open MongoDB shell by typing command ‘mongo’ or ‘mongo.exe’ from your installation directory location. Ensure that your server process for MongoDB is up and running.
3)    Type in command ‘use myDatabase’. It will open a database having name ‘myDatabase’ (It creates one if the database does not exist with this name).
4)    Insert rows within collection name testCollection –
You can choose any name for the collection and it will be automatically be created within the active database (myDatabase in our case), if the collection with the name does not exist.

db.testCollection.save( { k : 1 , m : 2 , n : 3 } )
db.testCollection.save( { k : 5 , l : 6 , m : 7, n : 8 } )
db.testCollection.save( { car : “myCar” , color : [ “red” , “green” , “blue” , “white” ] } )

point to note here is – each row can have altogether different set of data. While saving data of above type, MongoDB always creates and adds a unique identified with each record with key ‘_id’ and value of type ObjectId(“someNumber”) where ‘someNumber’ is potentially a unique number generated by MongoDB internally.

5)    Display all record from collection “testCollection” within active database–
db.testCollection.find()
6)    Search a record based on particular value from within data stored  –
db.testCollection.find ( { k : 1 } )
the above command will display result of the format –
{ “_id” : ObjectId(“123456a36b1ff789c12def3h”) ,  “k” : 1 , “m” : 2 , “n” : 3 }

There could be more complex search possible.

Previous articleCollection API
Next articlePMD Plug-in for Eclipse
I have spent almost 10 years playing around Java and related technologies. I love to write on different topics and would be very much willing to hear back your feedback/suggestions on them. This site is a medium to share my knowledge with the Java folks and grow further. My other interests include traveling, driving, swimming and dance. But yes, my web site has become my passion over the time :) I live in Scotland and travel to India often, my roots being there.

1 COMMENT

  1. Hey can we have little more information on this database like how it came around , some real time examples where this DB is used and so on…

LEAVE A REPLY