Indexing JSON data in MongoDB using PHP

·
  1. Install MongoDB (on OS X, can use Homebrew: brew install mongodb)
  2. Install the PHP module for communicating with MongoDB: pecl install mongo
  3. 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
  4. 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();
  5. Run a query using the HTTP interface:
    curl 'http://127.0.0.1:28017/tfl/stops/?filter_StopCode=1234'
  6. 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);