From: http://www.php100.com/html/webkaifa/PHP/PHPyingyong/2012/0326/10117.html
<? PHP // The default port 27017 is used to connect to the local machine. Of course, you can also connect to the 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 created before, you can also use $ M-> selectdb ("Comedy "); $ DB = $ M -> Comedy; // Select the collection set in comedy, which is equivalent to the table in RDBMS. You can also 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 $ collection $ Collection -> Insert ( $ OBJ ); // Add another element $ OBJ = Array ("Title" => "XKCD -". Date ('I: s'), "online" => True ); $ Collection -> Insert ( $ OBJ ); // Query all records $ Cursor = $ Collection -> Find (); // Traverse all documents in the Set Foreach ( $ Cursor As $ OBJ ){ Echo $ OBJ ["Title"]. "\ n" ;} // Delete all data $ Collection -> Remove (); // Delete HM as name $ Collection -> Remove ( Array ('Name' => 'hm' )); // Disconnect MongoDB $ M -> Close (); ?>