PHP + MongoDB notes

Source: Internet
Author: User
Tags mongodb server

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/>" ;}?>

 

  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.