MongoDb Installation
Download MongoDB from official MongoDB website. Choose Windows 32 bits or 64 bits.In Windows Explorer, locate the downloaded MongoDB .msi file, which typically is located in the default Downloads folder. Double-click the .msi file. A set of screens will appear to guide you through the installation process.
You may specify an installation directory if you choose the “Custom” installation option. These instructions assume that you have installed MongoDB to C:\mongodb.
MongoDB is self-contained and does not have any other system dependencies. You can run MongoDB from any folder you choose. You may install MongoDB in any folder (e.g.D:\test\mongodb).
1.Set up the MongoDB environment
MongoDB requires a data directory to store all data. MongoDB’s default data directory path is\data\db. Create this folder using the following commands from a Command Prompt:
md \data\db
You can specify an alternate path for data files using the --dbpath option to mongod.exe, for example:
C:\mongodb\bin\mongod.exe --dbpath "d:\test\mongo db data"
2.Start MongoDB
C:\mongodb\bin\mongod.exe
This starts the main MongoDB database process. The waiting for connections message in the console output indicates that the mongod.exe process is running successfully.
Depending on the security level of your system, Windows may pop up a Security Alert dialog box about blocking “some features” of C:\mongodb\bin\mongod.exe from communicating on networks. All users should select Private Networks, such as my home or work network and click Allow access. For additional information on security and MongoDB, please see the Security Documentation.
3.Connect to MongoDB
C:\mongodb\bin\mongo.exe
You should gain access right into the current MongoDB server. Now you can run Mongo shell commands to create databases, collections, store new data, or edit old data entries.
Run the following command to show all current databases on the server:
> show dbs
We can use the test database and inside create a new collection named “students” which will hold our Student Information. So first I will define a couple of variables inside the shell window.
First we consider only four students record with fields like firstname, middlename, lastname, class, division, roll_number and result.
- student1 = { firstname:"Santosh", middlename:"Gopinath", lastname:"shinde", class:"BE COMP", division:"A", roll_number:"1", result:"73.46"}
- student2 = { firstname:"Jayesh", middlename:"Balasaheb", lastname:"shinde", class:"BE COMP", division:"A", roll_number:"2", result:"68.46"}
- student3 = { firstname:"sahil", middlename:"santosh", lastname:"shinde", class:"BE COMP", division:"A", roll_number:"3", result:"83.86"}
- student4 = { firstname:"sachin", middlename:"Gopinath", lastname:"shinde", class:"BE COMP", division:"A", roll_number:"4", result:"73"}
If you execute above line one by one you get json data on your command prompt as like follows , For example student4 :
> student4 = { firstname:"sachin", middlename:"Gopinath", lastname:"shinde", cla
ss:"BE COMP", division:"A", roll_number:"4", result:"73"}
{
"firstname" : "sachin",
"middlename" : "Gopinath",
"lastname" : "shinde",
"class" : "BE COMP",
"division" : "A",
"roll_number" : "4",
"result" : "73"
}
- db.students.save(student1)
- db.students.save(student2)
- db.students.save(student3)
- db.students.save(student4)
You get output on each query as follows :
WriteResult({ "nInserted" : 1 })
To check data is properly inserted or not by using find() query command as like follows :
> db.students.find()
Setup the MongoDB PHP Drivers
All users on Windows will also need to edit their php.ini file. This can be accomplished directly from the WAMP context menu by clicking on the icon, then moving to PHP -> php.ini. You’ll need to add the same line of code except the filename should be php_mongo.dll.
extension=php_mongo.dll
Download mongodb driver for php from here .Find the latest release which supports your version of PHP (5.2, 5.3, 5.4,5.5) and download the .zip. Once you extract the folder find the extension which matches your version of PHP. In my PHP5 case I’ll dowanload php_mongo-1.5.5.zip and use php_mongo-1.5.5-5.2-vc9.dl and rename this to php_mongo.dll.
Make sure you use the thread safe version of the mongodb extension plugin. I stumbled upon the exact same errors while using the non-thread-safe versions.To download the right extension, see:http://www.php.net/manual/en/mongo.installation.php#mongo.installation.windows
Now place this file directly inside your PHP extensions directory located inC:\wamp\bin\php\php5.x\ext\. If you have this file moved over and the extension line of code added to your php.ini file then everything should be good to go! Restart your web server and open up a phpinfo() page to view the results.
Sample code to show our collection on web page using php code save as index.php in www/demo directory
<?php
// Config
$dbhost = '127.0.0.1';
$dbname = 'test';
// Connect to test database
$m = new MongoClient("mongodb://$dbhost");
$db = $m->$dbname;
// select the collection
$collection = $db->students;
// pull a cursor query
$cursor = $collection->find();
foreach($cursor as $document) {
var_dump($document);
}
?>
Thanks for sharing as it is an excellent post would love to read your future post
ReplyDeleteMongoDB Training Centers in Chenai