Skip to content

Instantly share code, notes, and snippets.

@tbsmcd
Created September 21, 2012 03:36
Show Gist options
  • Save tbsmcd/3759587 to your computer and use it in GitHub Desktop.
Save tbsmcd/3759587 to your computer and use it in GitHub Desktop.
<?php
$mongo = new Mongo('127.0.0.1:27017'); // local の場合。ポート 27017
// example が存在しない場合、 CREATE DATABASE 相当の操作は自動でやってくれる
$db = $mongo->selectDB('example');
// RDB でいうところの table
// test が存在しない場合(以下略)
$collection = $db->selectCollection('test');
// お、ドキュメント
$document = array(
'name' => 'Tomoaki Kanemoto',
'age' => 44,
'team' => 'Tigers',
'hometown' => array(
'prefecture' => 'Hiroshima',
'city' => 'Hiroshima',
),
);
$collection->insert($document);
$object = $collection->findOne();
var_dump($object);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment