This article mainly explains how to connect and operate mongodb in php. If you have any need, you can refer to the surging winds outside today. 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 to mongodb extension, please go back to see the http://www.jb51.net/article/31866.htm "target = _ blank> php to mongodb extension (first recognized as such) and php extensions to mongodb (initial).
Php connection to mongodb
The 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 blog
The Code is as follows:
$ Db = $ mongo-> blog;
Close Database
The Code is as follows:
$ Conn-> close ();
Select Operation set
$ Collection = $ db-> users;
Insert data
The Code is as follows:
$ User = array ('name' => 'caleng', 'city' => 'beijing ');
$ Collection-> insert ($ user );
Modify data
The Code is as follows:
$ Newdata = array ('$ set' => array ("city" => "shanghai "));
$ Collection-> update (array ("name" => "caleng"), $ newdata );
Delete data
The Code is as follows:
$ Collection-> remove (array ('name' => 'caleng'), array ("justOne" => true ));
Search for Data
Search for a piece of data
The Code is as follows:
$ Result = $ collection-> findone (array ("name" => "caleng "));
Query a list
The Code is as follows:
// Find the data created at a time greater than the specified 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 Query
The Code is as follows:
$ Cursor = $ collection-> find (array (
'Name' => array ('$ in' => array ('Joe', 'wendy '))
));
Group Query
The 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:
The Code is as follows:
[{"Category": "fruit", "items": ["apple", "peach", "banana"] },{ "category": "veggie ", "items": ["corn", "broccoli"]}]
The result is a two-dimensional array.
The 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.