Hello everyone. Over the past few weeks i have been playing with MongoDB. I would like to share with all of you what i learn so far. So let`s get started.
MongoDB is a cross-platform, open-source, document oriented database also called as NoSQL database. You can find more information about NoSQL here. Data in MongoDB is a document, which is a data structure composed of field and value pairs. These documents are similar to JSON objects. The values of fields may include other documents, arrays, and arrays of documents. For example
{ name: 'MongoDB', databaseType: 'nosql', keywords: ['nosql','document-oriented'] }
MongoDB stores the document on the disk in BSON format. BSON is binary representation of JSON documents. You can find more information about BSON here.
According to your operating system, you can download the appropriate version of MongoDB from here. I have windows 8.1 installed on my machine, so i downloaded the msi installer file. At the time of writing this article current production release version is 3.0.2. MongoDB uses odd-numbered version for development release that means if second number in version is odd then its development release otherwise a production release. For example second number for current production release 3.0.2 is zero which is an even number. At the time of writing this article current development release version is 3.1.1. which has 1 as second number which is an odd number. After installation directory structure looks like (F: 1):
Main two components in package are "mongod.exe" and "mongo.exe". mongod.exe is the main database process and mongo.exe is interactive javascript shell interface used to interact with database. MongoDB requires a data directory to store the data. Default directory path is /data/db or we can specify the data directory path using --dbpath parameter. I have created a folder called data to store the data files (F: 2). We can name the folder as we like.
Now we are all set to connect to MongoDB database. To connect to database open the command prompt and type the following command.
mongod.exe --dbpath "C:\ProgramFiles\MongoDB\Server\3.0\data"
As you can see in image (F: 3) the server process has started and waiting for connection on port number 27017. This is the default port for MongoDB. We can change this port number by specifying –port option while connecting to the server. We can also install MongoDB as a windows service. To install MongoDB as a windows service execute the following command in command prompt.
mongod --dbpath "C:\Program Files\MongoDB\Server\3.0\data" --logpath "C:\Program Files\MongoDB\Server\3.0\logs\log.txt" --logappend --install
Parameters Description:
After executing the command in command prompt let`s open the log file. Content of the log file is (F: 4):
As you can see in image (F: 4) that service has been installed successfully and we can start the service using command “net start MongoDB“. So let’s execute this command in command prompt.
Great! We have now successfully installed MongoDB as a windows service (F:5). We can cross check that the service has been started in Windows Local Services (F: 6).
As the server process has started, we can now connect to the database using mongo shell (mongo.exe). So let’s do it. We can start the mongo shell by typing mongo in command prompt. When we hit enter we will be prompted with blank window and a blinking cursor as shown in image below (F: 7).
MongoDB shell version in image (F: 7) specifies that we are using 3.0.1 version of MongoDB and connecting to specifies that we are connected to test database. In my next post we will discuss about the CRUD (Create Read Update Delete) operation in MongoDB.
Thankx dude it helped me a lot