I. Installing the MONGODB PHP extension
wget http://pecl.php.net/get/mongo-1.2.7.tgz//Download Expansion pack
Tar zxvf mongo-1.2.7.tgz
CD mongo-1.2.7
/usr/local/php/bin/phpize
./configure-with-php-config=/usr/local/php/bin/php-config
Make
Make install
Note: When in Ubuntu environment, need root permission, need to add sudo, so sudo makes && make install will report error, should be sudo makes && sudo make install is OK, because make install also needs root permission.
Second, Mongodb's crud API
1. Connect MongoDB to get the collection
$conn = new Mongoclient ("mongodb://127.0.0.1:27017");
$db = $conn->mydb;
$collection = $db->user;
2.Insert operation
$u 1 = array (' ID ' =>216, ' name ' = ' wfm11 ', ' age ' =>121, ' addr ' = ' 2beijing ');
$result = $collection->insert ($u 1);
Echo ' New user u1: '. $u 1[' _id ']. ' <br> ';
3.Update operation
$re = $collection->update ($where = array (' ID ' =>1), Array ('set '=>array (' id ' =>1, ' name ' = > ' AAA ', ' addr ' = ' Hangzhou '));
4.Select operation
$cursor = $collection,find ()->snapshot ();
foreach ($cursor as $id = = $value) {
echo $id. ': '. $value [' id ']. ' ---> '. $value [' name '];
Var_dump ($value);
}
$cursor = $collection, find (Array (' ID ' =>array (' $gt ' =>0, ' $lte ' =>21 ));
while ($cursor->hasnext ()) {
Var_dump ($cursor->getnext ());
}
$doc = $collection-FindOne ();
Var_dump ($doc);
5.count operation
Echo ' Count: '. $collection, COUNT (). ' <br> ';
Echo ' id=1 count '. $collection, COUNT (array (' ID ' =>1)). ' <br> ';
6.remove operation
$re = $collectionRemove (array (' ID ' =>269));
7. Add an index, specify the collation
Add index on name (ASC), age (DESC)
$collection-ensureindex (Array ("name" =-1, "age" =>1));