//连接数据库 $m = new MongoClient( "mongodb://127.0.0.1:27017" ); // 连接默认主机和端口为:mongodb://localhost:27017 //$m = new MongoClient("mongodb://root_mongo:xtwvfwlgr47782906@127.0.0.1:27017"); //连接设置了权限的mongo //$m = new MongoClient( "mongodb://example.com" ); // 连接远程数据库,默认端口为27017 //$m = new MongoClient( "mongodb://example.com:65432" ); // 连接远程数据库,端口号为指定的端口号。 $db = $m ->form1; // 获取名称为 "form1" 的数据库 //创建集合 $collection = $db ->createCollection( "runoob" ); echo "集合创建成功" ; //插入文档 $collection = $db ->form1; // 选择集合 $document = array ( "title" => "MongoDB" , "description" => "database" , "likes" => 100, "url" => "http://www.runoob.com/mongodb/" , "by" , "form1" ); $collection ->insert( $document ); echo "数据插入成功" ; // 更新文档 找到的第一条 $collection ->update( array ( "title" => "MongoDB" ), array ( '$set' => array ( "title" => "MongoDB 教程" ))); // 更新文档 找到的所有条 $collection ->update( array ( "title" => "MongoDB" ), array ( '$set' => array ( "title" => "MongoDB 教程" )), array ( 'multiple' => true) ); //查找文档 $cursor = $collection ->find(); var_dump( $cursor ); // 迭代显示文档标题 foreach ( $cursor as $document ) { echo $document [ "title" ] . " " ; } // 移除文档 $collection ->remove( array ( "title" => "MongoDB 教程" ), array ( "justOne" => true)); |