- Install MongoDB (on OS X, can use Homebrew: brew install mongodb)
- Install the PHP module for communicating with MongoDB: pecl install mongo
- Setup and start MongoDB:
#!/bin/bash
MONGO=/opt/homebrew/var/mongodb
mkdir -p ${MONGO}/data
mongod --dbpath=${MONGO}/data --logpath=${MONGO}/mongo.log
tail -f ${MONGO}/mongo.log
- Index some data (in this example, a bunch of JSON files from TfL):
<?php
$mongo = new Mongo;
$collection = $mongo->{'tfl'}->{'stops'};
foreach (glob('stops/*.js') as $file){
$json = json_decode(file_get_contents($file), TRUE); // TRUE = associative array
foreach ($json['Stops'] as $item){
$item['_id'] = $item['StopId']; // _id field = primary key
$item['StopCode'] = (int) $item['StopCode']; // make sure these are integers
$collection->insert($item, TRUE); // TRUE = throw an error if not successful
}
}
$collection->ensureIndex(array('StopCode' => 1)); // create an index on the StopCode field
$mongo->close();
- Run a query using the HTTP interface:
curl 'http://127.0.0.1:28017/tfl/stops/?filter_StopCode=1234'
- Run a query using PHP:
<?php
$mongo = new Mongo;
$collection = $mongo->tfl->stops;
$items = $collection->find(array('StopCode' => 1234));
foreach ($items as $item)
print_r($item);