Today, the wind is blowing outside. It is also a pleasure to write a blog in a warm hut. Well, let's talk a little bit about it. Today we will talk about how to connect to and operate mongodb using php. If you haven't followed the two phases, if you do not know how to install php extensions for mongodb, please go back to "php extensions for mongodb (first known)" and "php extensions for mongodb (first time)".
Php connection to mongodb
Copy codeThe Code is as follows: try {
$ Mongo = new Mongo ("mongodb: // username: password@127.0.0.1: 27017/db1 ");
} Catch (mongoonexception $ e ){
Print $ e-> getMessage ();
Exit;
}
Select Database blogCopy codeThe Code is as follows: $ db = $ mongo-> blog;
Close DatabaseCopy codeThe Code is as follows: $ conn-> close ();
Select Operation set
$ Collection = $ db-> users;
Insert dataCopy codeThe Code is as follows: $ user = array ('name' => 'caleng', 'city' => 'beijinging ');
$ Collection-> insert ($ user );
Modify dataCopy codeThe Code is as follows: $ newdata = array ('$ set' => array ("city" => "shanghai "));
$ Collection-> update (array ("name" => "caleng"), $ newdata );
Delete dataCopy codeThe Code is as follows: $ collection-> remove (array ('name' => 'caleng'), array ("justOne" => true ));
Search for Data
Search for a piece of dataCopy codeThe Code is as follows: $ result = $ collection-> findone (array ("name" => "caleng "));
Query a listCopy codeThe Code is as follows: // find the data created at a time greater 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); // the location where data is retrieved.
Var_dump ($ list_data );
In QueryCopy codeThe Code is as follows: $ cursor = $ collection-> find (array (
'Name' => array ('$ in' => array ('Joe', 'wendy '))
));
Group QueryCopy codeThe Code is 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 result:Copy codeThe Code is as follows: [{"category": "fruit", "items": ["apple", "peach", "banana"] },{ "category ": "veggie", "items": ["corn", "broccoli"]}]
The result is a two-dimensional array.Copy codeThe Code is as follows: array (
0 => array ("category" => "fruit", "items" => array ("apple", "peach", "banana ")),
1 => array ("category" => "veggie", "items" => array ("corn", "broccoli "))
)
Here we have written some simple operations. If you want to use php to better serve mongodb, read the manual.