The installers for MongoDB are available in both the 32-bit and 64-bit format. The 32-bit installers are good for development and test environments. But for production environments you should use the 64-bit installers. Otherwise, you can be limited to the amount of data that can be stored within MongoDB.
It is advisable to always use the stable release for production environments.
In this tutorial, you will learn –
The following steps can be used to install MongoDB on Windows
Step 1) Double click on the mongodb-win32-i386-3.0.7-signed.exe file which can be downloaded from the MongoDB website.
Step 2) Click on the Next button
Step 3) Click on the Next button to agree to the End User License Agreement.
Step 4) Click on the "complete" button to install all of the components. The custom option can be used to install selective components or if you want to change the location of the installation.
Step 5) Click on the Install button to start the installation.
Step 6) Click on the Finish button to complete the installation.
Drivers in MongoDB are used for connectivity between client applications and the database. For example, if you had Java program and required it to connect to MongoDB then you would require to download and integrate the Java driver so that the program can work with the MongoDB database.
- We are using the printjson method to print the variable to the screen.
Python Driver – For Python, first ensure that Python is present on the system. Then download the Python driver which is called the "pymongo" drivers. Then follow the following steps for the installation
Step 1) Launch the Installer and click on the Next button
Step 2) The installer will detect an existing Python installation. Click on the Next button to proceed.
Step 3) Click on the Next button to start the installation
Step 4) Click on the Next button to mark the installation as complete.
For the Ruby driver to be installed for MongoDB, follow the below steps
Step 1) Ensure Ruby is installed on the system
Step 2) Ensure gems is updated by issuing the command
gem update –system
Step 3) Install the mongo related drivers by issuing the below command
gem install mongo
There are tools in the market which are available for managing MongoDB. One such non-commercial tool is Robomongo.
Some of the features of Robomongo are given below:
- Full power of the Mongoshell
- Multiple shells
- Multiple results
Step 1) After installing the tool and launching it you need to first specify a connection
- Click on the Create link to create a new connection.
- Leave the address and port number as it is
- Click on the "Save" button
Step 2) Click on the Connect button
Once you connect to your instance, you can view all of your databases and collections in the interface. You can use this interface to insert documents, delete or update documents, find documents, etc.
Before starting the MongoDB server, the first key aspect is to configure the data directory where all the MongoDB data will be stored. This can be done in the following way
The above command 'md \data\db' makes a directory called \data\db in your current location.
MongoDB will automatically create the databases in this location, because this is the default location for MongoDB to store its information. We are just ensuring the directory is present, so that MongoDB can find it when it starts.
The import of data into MongoDB is done using the "mongoimport" command. The following example shows how this can be done.
Step 1) Create a CSV file called data.csv and put the following data in it
So in the above example, we are assuming we want to import 3 documents into a collection called data. The first row is called the header line which will become the Field names of the collection.
Step 2) Issue the mongo import command
- We are specifying the db option to say which database the data should be imported to
- The type option is to specify that we are importing a csv file
- Remember that the first row is called the header line which will become the Field names of the collection, that is why we specify the –headerline option. And then we specify our data.csv file.
The output clearly shows that 3 documents were imported into MongoDB.
Exporting MongoDB is done by using the mongoexport command
- We are specifying the db option to say which database the data should be exported from.
- We are specifying the collection option to say which collection to use
- The third option is to specify that we want to export to a csv file
- The fourth is to specify which fields of the collection should be exported.
- The –out option specifies the name of the csv file to export the data to.
The output clearly shows that 3 records were exported from MongoDB.
One can configure the mongod server instance to startup with a configuration file. The configuration file contains settings that are equivalent to the mongod command-line options.
For example, supposed you wanted MongoDB to store all its logging information to a custom location then follow the below steps
Step 1) Create a file called, "mongod.conf" and store the below information in the file
- The first line of the file specifies that we want to add configuration for the system log file, that is where the information about what the server is doing in a custom log file.
- The second option is to mention that the location will be a file.
- This mentions the location of the log file
- The logAppend: "true" means to ensure that the log information keeps on getting added to the log file. If you put the value as "false", then the file would be deleted and created fresh whenever the server starts again.
Step 2) Start the mongod server process and specify the above created configuration file as a parameter. The screenshot of how this can be done is shown below
Once the above command is executed, the server process will start using this configuration file, and if you go to the /etc. directory on your system, you will see the mongod.log file created.
The below snapshot shows an example of what a log file would look like.