Php extension for mongodb (small test)

Source: Internet
Author: User

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 blog
Copy codeThe Code is as follows:
$ Db = $ mongo-> blog;

Close Database
Copy codeThe Code is as follows:
$ Conn-> close ();

Select Operation set
$ Collection = $ db-> users;
Insert data
Copy codeThe Code is as follows:
$ User = array ('name' => 'caleng', 'city' => 'beijing ');
$ Collection-> insert ($ user );

Modify data
Copy codeThe Code is as follows:
$ Newdata = array ('$ set' => array ("city" => "shanghai "));
$ Collection-> update (array ("name" => "caleng"), $ newdata );

Delete data
Copy codeThe Code is as follows:
$ Collection-> remove (array ('name' => 'caleng'), array ("justOne" => true ));

Search for Data
Search for a piece of data
Copy codeThe Code is as follows:
$ Result = $ collection-> findone (array ("name" => "caleng "));

Query a list
Copy codeThe 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
Copy codeThe Code is as follows:
$ Cursor = $ collection-> find (array (
'Name' => array ('$ in' => array ('Joe', 'wendy '))
));

Group Query
Copy 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.

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.