Introduction to MongoDB
MongoDB is a open-source document-oriented database written in C++ and C and licensed under the GNU Affero General Public License and the Apache Licenses. It is classified as a NoSQL database using JSON-like formatted documents for the data model. Although there are several other NoSQL databases on the market today, mongoDB is by far, the most popular one.
Instead of using tables for storage of its data like a traditional RDBMS, mongoDB stores its data in collections. A collection holds one or more documents, which are the equivalent of records or rows of data in a relational database system. Each of these documents contain one or more fields, which corresponds to columns in a traditional table in a RDBMS.
There are several differences though, chief among these is the fact that in traditional tables in relational databases a table shares the same set of columns. However, documents in a collection may have a different set of fields. That is, documents do not necessarily have to have the same set of fields. MongoDB collections work just fine if you add fields to some documents and not others. We will demonstrate this later, in another set of tutorials.
1. Downloading MongoDB
MondoDB offers several binary distributions, both in 32 & 64-bit flavors for various operating systems. They may be found at the following URL: http://www.mongodb.org/downloads or from their main website click the on downloads link.
Since I am running on Windows 64-bit operating system, I would choose the first option, highlighted in RED. However, mongoDB also supports Linux, Mac OS and Sun/Solaris operating systems.
Please download the appropriate binary for your computer and install mongoDB. In my case, I chose to download the 64-bit MSI version (Microsoft Installer).
2. Creating the Data Directory
By default MongoDB stores its data in C:datadb directory, however, since mongo does not automatically create it for you it may be necessary to manually create this data directory.
Using a DOS command prompt, we can issue the make directory (mkdir) command to create the necessary directory structure. To ensure that the directory was created correctly, I simply change directory (cd) to the newly created directory structure.
3. Running MongoDB on Windows
On my system, mongoDB was installed in Program Files in a folder called MongoDB 2.6 Standard. You will need to ensure you are in the bin folder. Start the MongoDB daemon process for the system. This will handle all data requests, manage the format of the data, and perform the necessary background data management.
C:UsersAmaury>cd Program FilesMongoDB 2.6 Standard C:Program FilesMongoDB 2.6 Standard>cd bin C:Program FilesMongoDB 2.6 Standardbin>mongod.exe
4. Interacting with MongoDB
Let’s bring up the MongoDB shell, called mongo.exe, in order to start. Once you start it, you should have a command prompt with a greater than sign (>). It is in this administrative shell, that we can begin to interact with MongoDB.
5. Getting Server Status on MongoDB
Provides a multitude of detail on mongoDB instance. This information will vary depending on the version, operating system platform and kind of node you are currently running. For a complete list, please use the URL: http://docs.mongodb.org/manual/reference/command/serverStatus/.
6. Creating a database in MongoDB
In MongoDB, we don’t really create databases. Switch to the new database you wish to use. Let’s create a new database called library.
Then begin inserting documents into the collection you wish to use. We will insert documents into the books collection.
7. Insert Documents into MongoDB
You may have noticed that the last document in the collection differs slightly from all the others. It has the following format:
In addition, it does NOT contain the isbn that all the other documents contain. In MongoDB, this is fine as the schema need not be homogeneous as it does in standard relational databases.
8. Selecting all documents in the collection
Now that we’ve inserted all six documents into our collection we would like to see what is in the database. The way we do this in Mongo is by using the find command.
Does anything look different about the documents that are being returned by MongoDB? You may have noticed that mongo added the _id field to all of our objects.
That is because in mongo, all documents must have an _id field with a unique value. Had we wanted to, we could have added the _id field ourselves and specified unique values for each of the documents we inserted.
9. Making the format easier to read
If the output is a little hard to read, no worries. MongoDB has a nice little command to format the output using the pretty method.
10. What’s Next
Believe it or not, we have covered quite bit of material for the very lesson on MongoDB. In our next tutorials, we will continue delving deeper into MongoDB syntax and create a more involved examples.
References
Please Share Us on Social Media






Leave a Reply