PHP operation MongoDB API most methods

Source: Internet
Author: User
Tags findone php mongodb

<?PHP/*PHP MongoDB * All curd operation * @author: Xiaojiang * @date: 2014-10-27*///view Mongo class version 1.30 later versions using Mongoclient//mongo::version;/*Connection*/    $m=NewMongo ("Mongodb://cms:[email protected]:55555/banggocms"); //Connect multiple units//$m = new Mongo ("mongodb://localhost:27017,localhost:27018");//Select Database$db=$m->selectdb ("Banggocms"); //$db = $m->banggocms;$col=$db-dbeleComment2014;/*New*//*$opt = Array (' Fsync ' + 1//default is False, False when log configuration is on and J parameters are similar otherwise the write operation waits until the data is fully synchronized to the hard disk ... true ignores execution results//.  .. )*///$ret = $col->insert (Array (' PID ' =>2, ' content ' = ' test123 '), $opt);/*Update*/    //$set//Modify properties//$ret = $col->update (' pid ' = 2), array (' $set ' = = ' Array (' content ' = ' 123456 '))); $unset//Delete an attribute $unset//$ret = $col->update (' pid ' = 2), array (' $unset ' = = Array (' content ' =>1    , ' contents ' =>1)); $push//Update a record for a property new property//$ret = $col->update (' pid ' = 2), array (' $push ' = = Array (' Co    Ntents ' = = Array (' V ' =>1))); $pushall//Bulk Add data//$ret = $col->update (' pid ' = 2), array (' $pushAll ' = = Array (' contents ' = Arra    Y (1,2,3,4))); The $addToSet//and push functions are similar to $push, and the $addtoset guarantees the uniqueness of the element, preventing duplicate additions//$ret = $col->update (' pid ' + 2), Array (' $    Addtoset ' = = Array (' contents ' = + 1)); $pop//Deletes the first record in the field array.  MongoDB 1.1 and later versions can be used 2 or-two to delete two,//$ret = $col->update (' pid ' = + 2), array (' $pop ' = = Array (' contents ' =    -1)); $pull index//$ret = $col->update (' pid ' = 2), array (' $pull ' = = array(' contents ' + 2))    );  $PULLALL//$ret = $col->update (Array (' pid ' = 2), array (' $pullAll ' = = Array (' contents ' = = Array (1,2,3,4)    ) ) );   $inc $ represents itself in the subkey//$ret = $col->update (' contents ' = + 3), array (' $inc ' = = Array (' contents.$ ' = + 100)    ) ); $ represents the match record in the subkey itself//delete//$ret = $col->update (' contents ' + 8), array (' $unset ' = = Array (' Content        s.$ ' = 1));        Modify//$ret = $col->update (Array (' contents ' = + 2), array (' $set ' = = Array (' contents.$ ' = + 11))); Add PS: Updated node if array type//$ret = $col->update (' contents.v ' = +), array (' $push ' = = Array (' Conten Ts.$.test ' = = Array (' t1 ' =>1, ' T2 ' =>2)));    /*Delete*/     //$ret = $col->remove (Array (' pid ' = 2)); Delete only one//$ret = $col->remove (Array (' pid ' = 2), Array ("justone" = True));/*Enquiry*/ //$ret = $col->find (' pid ' =>2, ' content ' = ' test123 '));//subquery//$ret = $col->findone (' pid ' =    >2, ' contents ' = 1));    $ret = $col->findone (Array (' PID ' =>2, ' contents.t ' + 5)); Get total//$t = $ret->count (),//$ret = $col->find (Array (' PID ' =>2)->skip (10);//Sort//$ret = $col Find (Array (' PID ' =>2))->sort (Array ("a" = 1));//return field//$ret = $col->find (Array (' PID ' =>2), Array (' Content ');//$ret = $col->find (' pid ' =>2), array (' content ' = 0);    Ignore field//Return object PHP >= 5.1 iterator_to_array//var_dump (Iterator_to_array ($ret));//otherwise//foreach ($ret as $v) {// Var_dump ($v);//}//$ret = $col->findone (Array (' PID ' =>2, ' content ' = ' test123 '));//returns an array directly?>

PHP operation MongoDB API most methods

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.