Skip to main content

MongoDb with PHP on Windows

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


To start MongoDB, run mongod.exe. For example, from the Command Prompt:
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.
step1mongod.jpg
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

To connect to MongoDB through the mongo.exe shell, open another Command Prompt:
C:\mongodb\bin\mongo.exe
step2mongo.jpg
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.
  1. student1 = { firstname:"Santosh", middlename:"Gopinath", lastname:"shinde", class:"BE COMP", division:"A", roll_number:"1", result:"73.46"}  

  1. student2 = { firstname:"Jayesh", middlename:"Balasaheb", lastname:"shinde", class:"BE COMP", division:"A", roll_number:"2", result:"68.46"}

  1. student3 = { firstname:"sahil", middlename:"santosh", lastname:"shinde", class:"BE COMP", division:"A", roll_number:"3", result:"83.86"}

  1. 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"
}
We need to use the MongoDB .save() command for saving new data and creating new collections.
  1. db.students.save(student1)
  2. db.students.save(student2)
  3. db.students.save(student3)
  4. 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
Most likely you'll need the VC9 (Thread Safe) version from here.
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);
}
?>

Comments

  1. Thanks for sharing as it is an excellent post would love to read your future post

    MongoDB Training Centers in Chenai

    ReplyDelete

Post a Comment

Popular posts from this blog

What exactly means MVW design pattern ?

What is a MVW framework? The abbreviation stands for 'Model - View - Whatever'.  Well there are many different JavaScript frameworks available , all invented for the same purpose. They all try to separate the presentation logic from the business logic where JavaScript holds the model and logic, and html the presentation layer. Quick overview : You can change the model without changing the view and vice-versa Unit testing is easy These are the core benefits of using such a framework. By separating presentation and business logic changes to the code are easier to implement and take less time. Another benefit is that code can be reused from a controller thus saving more time. Angularjs makes the code also shorter in comparison to other frameworks, which improves code stability. Less code means minor potential for bugs. For several years +AngularJS was closer to MVC (or rather one of its client-side variants), but over time and thanks to many refactorings...

File Upload & Download With ng-cordova File Transfer Plugin In Ionic Framework

Using the AngularJS extension set , ngCordova , with Ionic Framework and the Apache Cordova File Transfer plugin, you can easily upload files to a remote server and download files from a remote server. 1.Create the project ionic start Test blank cd Test ionic platform add android 2.Add Plugin org.apache.cordova.file-transfer https://github.com/apache/cordova-plugin-file-transfer This plugin allows you to upload and download files. This plugin defines global FileTransfer, FileUploadOptions Constructors. Although in the global scope, they are not available until after the deviceready event . Installation cordova plugin add cordova plugin add cordova-plugin-file-transfer     2.    org.apache.cordova.file             https://github.com/apache/cordova-plugin-file             This plugin implements a Fi...

Steps To Create Cordova Plugin

Introduction A plugin is a package of injected code that allows the Cordova webview within which the app renders to communicate with the native platform on which it runs. Plugins comprise a single JavaScript interface along with corresponding native code libraries for each supported platform. Due to following reason, we will go for plugin development To get the native capabilities of the device in the cordova based app Build the common functionality across the number of Cordova application(Reusability) Prerequisites For the creation of plugin you need to install plugman . npm install -g plugman Plugin Structure Decide Plugin ID & Name The plugin ID should be unique Plugin name should follow the convention “cordova.plugin.[your.plugin.name]” Create the structure Use the following command to create the plugin with plugin ID “cordova.plugin.test” and name “Test”. plugman create --name Test --...