Mongodbphp and mongodb. Mongodbphp: mongodb first installs extensions before using mongodb 1. connect to the database try {$ mongonewMongoClient (); $ db $ mongo-mydb; var_dump ($ db );} catch (MongoCon mongodb php, mongodb
Install the extension before using mongodb
1. connect to the database
try { $mongo = new MongoClient(); $db = $mongo->mydb; var_dump($db);} catch (MongoConnectionException $e) { echo $e->getMessage();}
This code can connect to the mydb Database. if the database does not exist, it is automatically created.
II. create a set
try { $mongo = new MongoClient(); $db = $mongo->mydb; $mycol = $db->createCollection('mycol'); var_dump($mycol);} catch (MongoConnectionException $e) { echo $e->getMessage();}
This code creates a set of mycol.
3. Insert a document
Insert () is used in mongodb to insert a document.
try { $mongo = new MongoClient(); $db = $mongo->mydb; $mycol = $db->mycol; $document = array('name' => 'test1' , 'sex' => 'formale' , 'age' => 20); $res = $mycol->insert($document); var_dump($res);} catch (MongoConnectionException $e) { echo $e->getMessage();}
Output:
array (size=4) 'ok' => float 1 'n' => int 0 'err' => null 'errmsg' => null
4. search for documents
Mongodb uses find () to find documents
try { $mongo = new MongoClient(); $db = $mongo->mydb; $mycol = $db->mycol; $mongoCursor = $mycol->find(); foreach ($mongoCursor as $document) { var_dump($document); }} catch (MongoConnectionException $e) { echo $e->getMessage();}
Result:
array (size=4) '_id' => object(MongoId)[7] public '$id' => string '56c28a793b22cf5415000029' (length=24) 'name' => string 'test1' (length=5) 'sex' => string 'formale' (length=7) 'age' => int 20
5. update documents
Use update () to update documents
try { $mongo = new MongoClient(); $db = $mongo->mydb; $mycol = $db->mycol; $mycol->update(array('name'=>'test1') , array('$set'=>array('age' => 21))); $mongoCursor = $mycol->find(); foreach ($mongoCursor as $document) { var_dump($document); }} catch (MongoConnectionException $e) { echo $e->getMessage();}
Result
array (size=4) '_id' => object(MongoId)[7] public '$id' => string '56c28a793b22cf5415000029' (length=24) 'name' => string 'test1' (length=5) 'sex' => string 'formale' (length=7) 'age' => int 21
6. delete documents
try { $mongo = new MongoClient(); $db = $mongo->mydb; $mycol = $db->mycol; $mycol->remove(array('name'=>'test1')); $mongoCursor = $mycol->find(); foreach ($mongoCursor as $document) { var_dump($document); }} catch (MongoConnectionException $e) { echo $e->getMessage();}
Remove method
public bool|array MongoCollection::remove ([ array $criteria = array() [, array $options = array() ]] )
Options:
"W": the level of the thrown exception. the default value is 1;
"JustOne": only one matching record can be deleted;
"Fsync": Boolean, defaultsFALSE
. Forces the insert to be synced to disk before returning success. IfTRUE
, An acknowledged insert is implied and will override settingWTo0.
"Timeout": Integer, defaultsExport cursor: $ 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 will be thrown.
......
For other methods, see The php Manual: http://php.net/manual/zh/book.mongo.php
Http://www.bkjia.com/PHPjc/1099829.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1099829.htmlTechArticlemongodb php, mongodb first install extensions, then you can use mongodb 1, connect to the database try {$ mongo = new MongoClient (); $ db = $ mongo-mydb; var_dump ($ db);} catch (catch con...