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:
    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):
    $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
  5. Run a query using the HTTP interface:
    curl ''
  6. Run a query using PHP:
    $mongo = new Mongo;
    $collection = $mongo->tfl->stops;
    $items = $collection->find(array('StopCode' => 1234));
    foreach ($items as $item)