PHP extensions to MongoDB (small test) _javascript tips

Source: Internet
Author: User
Tags mongodb prev

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:

$db = $mongo->blog;

Close Database
Copy Code code as follows:

$conn->close ();

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.

Related Article

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.