Today outside the whistling wind, can be in a warm cabin writing blog is also a happy North drift. Okay nonsense not to say, today mainly say PHP connection, Operation MongoDB, if you do not see the content of two, do not know how to install the expansion of PHP to MongoDB please return to see the "PHP to MongoDB expansion (the first known)" and " PHP's extension to MongoDB (CUB).
PHP Connection MongoDB
Copy Code code as follows:
try {
$mongo = new MONGO ("MONGODB://USERNAME:PASSWORD@127.0.0.1:27017/DB1");
}catch (Mongoconnectionexception $e) {
Print $e->getmessage ();
Exit
}
Select Database Blog
Copy Code code as follows:
Close Database
Copy Code code as follows:
Select an Action collection
$collection = $db->users;
Inserting data
Copy Code code as follows:
$user = Array (' name ' => ' Caleng ', ' City ' => ' Beijing ');
$collection->insert ($user);
modifying data
Copy Code code as follows:
$newdata = Array (' $set ' => Array ("City" => "Shanghai"));
$collection->update (Array ("name" => "Caleng"), $newdata);
Delete data
Copy Code code as follows:
$collection->remove (Array (' name ' => ' Caleng '), Array ("Justone" => true);
Find data
Find a piece of data
Copy Code code as follows:
$result = $collection->findone (Array ("name" => "Caleng"));
Query a list
Copy Code code as follows:
Find the data that was created longer than a certain time
$start = 1;
$counditionarray =array ("CTime" =>array (' $gt ' =>1337184000));
$list _data = $this->game_handle->find ($counditionarray);
$total = $this->game_handle->count ($counditionarray);
$list _data->limit ($count); Data End Position
$list _data->skip ($start); Where the data began to take place
Var_dump ($list _data);
In query
Copy Code code as follows:
$cursor = $collection->find (Array (
' Name ' => array (' $in ' => array (' Joe ', ' Wendy ')
));
Group Query
Copy Code code as follows:
$collection->insert (Array ("category" => "fruit", "name" => "Apple"));
$collection->insert (Array ("category" => "fruit", "name" => "Peach"));
$collection->insert (Array ("category" => "fruit", "name" => "Banana"));
$collection->insert (Array ("category" => "veggie", "name" => "Corn"));
$collection->insert (Array ("category" => "veggie", "name" => "Broccoli"));
$keys = Array ("category" => 1);
$initial = Array ("Items" => array ());
$reduce = "function (obj, prev) {Prev.items.push (obj.name);}";
$g = $collection->group ($keys, $initial, $reduce);
Echo Json_encode ($g [' retval ']);
Output results:
Copy Code code as follows:
[{"category": "Fruit", "items": ["apple", "peach", "banana"]},{"category": "Veggie", "Items": ["corn", "broccoli"]}]
You can see that the result is a two-dimensional array
Copy Code code as follows:
Array
0 => Array ("category" => "fruit", "Items" =>array ("apple", "peach", "banana")),
1 => Array ("category" => "veggie", "Items" =>array ("corn", "broccoli"))
)
Here's a few simple things to do, and if you want to use PHP for a better role MongoDB read the manual.