Copy Code code as follows:
<?php
This is the default connection to the local 27017 port, of course, you can also connect remote host such as 192.168.0.4:27017, if the port is 27017, the port can be omitted
$m = new Mongo ();
Select the comedy database, if the database is not previously created automatically, you can also use $m->selectdb ("comedy");
$db = $m->comedy;
Select the collection set within the comedy, which is equivalent to the table in the RDBMS, also-you can use
$collection = $db->collection;
$db->selectcollection ("collection");
Add an Element
$obj = Array ("title" => "Calvin and hobbes-". Date (' i:s '), "author" => "Bill Watterson");
Add $obj to the $collection collection
$collection->insert ($obj);
Add another element
$obj = Array ("title" => "xkcd-". Date (' i:s '), "online" => true);
$collection->insert ($obj);
Query all the records
$cursor = $collection->find ();
Iterate through the documents in all collections
foreach ($cursor as $obj)
{
echo $obj ["title"]. "<br/>\n";
}
Delete all data
$collection->remove ();
Delete Name as HM
$collection->remove (' name ' => ' HM '));
Disconnect MongoDB Connection
$m->close ();
?>