There is no time to write the code, and there are comments in it.
<? PHP header ("Content-Type: text/html; charset = UTF-8"); // solves the problem of garbled characters when the inserted data contains Chinese characters, note that the format of test1.php file storage should also be utf8 format $ Mongo = new Mongo ("localhost: 27017"); // connect to the MongoDB server $ db = $ mongo-> gywtest; // select a database $ collection = $ db-> student; // select a collection, set $ Doc = array ("stuname" => "Xiaoming", "stunumber" => "1402040455", "Age" => 28, "Major" => ""); // construct an associated array and insert it to the collection. Try {$ collection-> insert ($ DOC ); // insert a record echo "insert OK";} c Atch (exception $ e) {echo $ e-> getmessage () ;}?>
<? PHP header ("Content-Type: text/html; charset = UTF-8"); // solves the problem of garbled characters when the inserted data contains Chinese characters, note that the format of test1.php file storage should also be utf8 format $ Mongo = new Mongo ("localhost: 27017"); // connect to the MongoDB server $ db = $ mongo-> gywtest; // select a database $ collection = $ db-> student; // select a collection, if you want to use a cursor to query a database record in a relational database set/* $ cursor = $ collection-> Find (); // by default, get all records foreach ($ cursor as $ row) {// The record is equivalent to a two-dimensional array foreach ($ row as $ key => $ value) {// traverse the field echo $ key in each row. ":". $ value. "";} Echo "<br/>";} * // condition query/* $ query = array ("Age" => 21 ); $ cursor = $ collection-> Find ($ query); foreach ($ cursor as $ row) {// The record is equivalent to a two-dimensional array foreach ($ row as $ key => $ value) {// traverses the field echo $ key in each row. ":". $ value. "";} echo "<br/> ";} * // query by range 21 <age <= 28 $ query = array ("Age" => array ('$ GT' => 21, '$ LTE' => 28); $ cursor = $ collection-> Find ($ query); foreach ($ cursor as $ row) {// The record is equivalent to a two-dimensional array foreach ($ row as $ key => $ Value) {// traverse the field echo $ key. ":". $ value. "";} echo "<br/>" ;}?>