MongoDB bannerMongoDB banner

I have no experience with NoSQL databases and was curious to learn a bit more, with the goal of using MongoDB on a Heroku project. Here is a brief outline of my experiment with PHP and MongoDB on my dev machine:

1. Setting up MongoDB

Download and install MongoDB. Add it to your environment path; something like: C:\Program Files\MongoDB\Server\3.0\bin

2. Configuring MongoDB

MongoDB must be running on your PC. Open a command window / Powershell and simply type mongod

Here is a great tutorial: https://www.mongodb.com/docs/manual/tutorial/install-mongodb-on-windows/

3. Configuring PHP

Download the PHP/MongoDB dll — I downloaded the threadsafe one

In your php.ini, add: extension=php_mongo.dll

Restart Apache

4. Writing your app

I used the following tutorial to write my first PHP / MongoDB script: http://blog.mongodb.org/post/24960636131/mongodb-for-the-php-mind-part-1?_ga=1.164454587.560227907.1446129047

This is my little PHP/MongoDB app. Remember that everything kinda happens on the fly. This feels very odd at first if like me you are used to spending time setting up your MySQL tables before you even start coding your app. I soon found it to be quite liberating:

<?php
date_default_timezone_set('Africa/Johannesburg');
echo '<!DOCTYPE html>';
echo '<html lang="en">';
echo '<head>';
	echo '<meta charset="utf-8">';
	echo '<title>Hello World! &mdash; PHP &amp; MongoDB</title>';
	echo '<!--[if IE]>';
		echo '<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>';
	echo '<![endif]-->';
echo '<style>';
echo '* {font-family: Arial;}';
echo '</style>';
echo '</head>';
echo '<body>';

echo '<h1>Hello World</h1>';
echo '<form action="'.$_SERVER['PHP_SELF'].'" method="POST">';
echo '<fieldset>';
echo '<p><label>First Name </label><input type="text" name="first_name" placeholder="First Name" /></p>';
echo '<p><label>Last Name </label><input type="text" name="last_name" placeholder="Last Name" /></p>';
echo '<p><label>Tags </label><input type="text" name="tags" placeholder="Comma separated list of tags" /></p>';
echo '</fieldset>';
echo '<input type="submit" name="submit" value="Add" />';
echo '</form>';

// Configuration
$dbhost = 'localhost';
$dbname = 'test';

// Connect to test database
$m = new Mongo("mongodb://$dbhost");
$db = $m->$dbname;

// Get the users collection
$c_users = $db->users;

	if($_POST) {
		$since = date("Y-m-d H:i:s");
		$first_name = $_POST['first_name'];
		$last_name = $_POST['last_name'];
		$tagsArr = explode(",",$_POST['tags']);
		$user = array(
			'first_name' => $first_name,
			'last_name' => $last_name,
			'member_since' => $since,
			'tags' => array($tagsArr)
		);	

		// Insert this new document into the users collection
		$c_users->save($user);
	}

	// get the collection
	$user = $c_users->find();
	foreach ( $user as $id => $value ) {
		echo $id;
		echo '<pre>';
		var_dump( $value );
		echo '</pre>';
		echo '<hr />';
	}

echo '</body>';
echo '</html>';

References:

  1. Install MongoDB Community Edition on Windows (2023) Mongodb.com. Available at: https://www.mongodb.com/docs/manual/tutorial/install-mongodb-on-windows/ (Accessed: 29 March 2023).
  2. MongoDB for the PHP Mind, Part 1 | MongoDB Blog (2023). Available at: https://www.mongodb.com/blog/post/mongodb-for-the-php-mind-part-1?_ga=1.164454587.560227907.1446129047 (Accessed: 29 March 2023).

By foxbeefly

PHP / MySQL Developer. HTML, CSS and some JavaScript.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from stylus

Subscribe now to keep reading and get access to the full archive.

Continue reading