find($doc)->toArray(); foreach($da as $d) { echo json_encode($d); /* echo sprintf("Id : %d\r\n", $d['id']); echo sprintf("Customer : %s\r\n", $d['customer']); foreach($d['items'] as $d1) { echo sprintf(" %2d %-30s %10s %4d %10s\r\n", $d1['no'], $d1['name'], $d1['price'], isset($d1['quantity']) ? $d1['quantity'] : 1, isset($d1['note']) ? $d1['note'] : ''); } */ } } // open $client = new Client('mongodb://localhost:27017'); $db = $client->TestDB; $col = $db->php_json; // setup $s1 = '{ "id": 1, "customer": "A A", "items": [ { "no": 1, "name": "A good PHP book", "price": "19.95" } ] }'; echo "$s1\r\n"; $col->insertOne(json_decode($s1)); $s2 = '{ "id": 2, "customer": "B B", "items": [ { "no": 1, "name": "ASUS MB", "price": "249.00" }, { "no": 1, "name": "i5 CPU", "price": "299.00", "note": "handle with care" }, { "no": 1, "name": "4 GB kit", "price": "249.00", "quantity": 4} ] }'; echo "$s2\r\n"; $col->insertOne(json_decode($s2)); // query $d = array(); // all dump($col, $d); $d = array('id' => 2); // where id=2 dump($col, $d); $d = array('customer' => 'A A'); // where customer='A A' dump($col, $d); $d = array('customer' => array('$regex' => 'A.*'));// where customer like 'A%' dump($col, $d); $d = array('items.name' => 'i5 CPU'); // where items.name='i5 CPU' dump($col, $d); $d = array('items.name' => array('$regex' => '.*PHP.*')); // where items.name like '%Java%' dump($col, $d); $d = array('id' => array('$in' => array(1, 2))); // where id in (1,2) dump($col, $d); ?>