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