MongoDB php,mongodb_php Tutorial

Source: Internet
Author: User
Tags mongoclient

MongoDB Php,mongodb


Install the extension first before you can use MongoDB

First, connect the database

Try {    $mongonew  mongoclient ();     $db $mongo, mydb;     Var_dump ($dbcatch$e) {    echo$e getMessage ();}

The code can connect to the MyDB database and is created automatically if the database does not exist.

Second, create the collection

Try {    $mongonew  mongoclient ();     $db $mongo, mydb;     $mycol $db->createcollection (' MyCol ');     Var_dump ($mycolcatch$e) {    echo$e getMessage ();}

The code can create a collection of MyCol.

Third, insert the document

Use Insert () in MongoDB to insert the document.

Try {    $mongonew  mongoclient ();     $db $mongo, mydb;     $mycol $db, MyCol;     $document Array (' name ' = ' test1 ', ' sex ' = ' formale ', ' age ' = ');     $res $mycol->insert ($document);     Var_dump ($rescatch$e) {    echo$e getMessage ();}

Output:

Array (size=4)   float 1  ' n ' = = int 0  null  null

Iv. Finding documents

MongoDB uses find () to find documents

Try {    $mongonew  mongoclient ();     $db $mongo, mydb;     $mycol $db, MyCol;     $mongoCursor $mycol, find ();     foreach ($mongoCursoras$document) {        var_dump($document );     Catch $e {    echo$e-GetMessage ();}

Results:

Array (size=4)  ' _id ' = =     object(MongoId) [7      ]publicstring ' 56c28a793b22cf5415000029 ' (length=24)  string ' test1 ' (length=5)    String ' Formale ' (length=7)  ' age ' = int 20

V. Update the DOCUMENTATION

Update the document using update ()

Try {    $mongonew  mongoclient ();     $db $mongo, mydb;     $mycol $db, MyCol;     $mycol->update (arrayarray(' $set ' = =array(' age ')]) ;     $mongoCursor $mycol, find ();     foreach ($mongoCursoras$document) {        var_dump($document );     Catch $e {    echo$e-GetMessage ();}

Results

Array (size=4)  ' _id ' = =     object(MongoId) [7      ]publicstring ' 56c28a793b22cf5415000029 ' (length=24)  string ' test1 ' (length=5)    String ' Formale ' (length=7)  ' age ' = int 21

Vi. Deletion of documents

Try {    $mongonew  mongoclient ();     $db $mongo, mydb;     $mycol $db, MyCol;     $mycol->remove (array(' name ' = ' test1 '));     $mongoCursor $mycol, find ();     foreach ($mongoCursoras$document) {        var_dump($document );     Catch $e {    echo$e-GetMessage ();}

Remove method

 Public bool| Array Array $criteria Array Array $options Array () ]] )

Options to delete:

"W": the level of exception thrown, default is 1;

"Justone": Delete at most one matching record;

"fsync": Boolean, defaults to FALSE . Forces the insert to is synced to disk before returning success. If TRUE , an acknowledged inserts is implied and would override setting W to 0.

"timeout": Integer, defaults to mongocursor:: $timeout. If "Safe" is set, this sets how long (in milliseconds) for the client to wait for a database response. If the database does not respond within the timeout period, Amongocursortimeoutexception would be thrown.

......

Other methods can be found in the PHP manual: http://php.net/manual/zh/book.mongo.php

http://www.bkjia.com/PHPjc/1099829.html www.bkjia.com true http://www.bkjia.com/PHPjc/1099829.html techarticle MongoDB Php,mongodb First installs the extension before you can use MongoDB one, connect the database try {$mongo = new mongoclient (), $db = $mongo-mydb; Var_dump ($db );} catch (Mongocon ...

  • 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.