PHP operation MongoDB Database specific sample introduction (increase, delete, change, check) (vi)

Source: Internet
Author: User

PHP Operation MongoDB:

PHP need to play module to operate MongoDB
Official website can download: Http://pecl.php.net/package/mongo download

MongoDB is set to user-authorized startup mode

PHP Manual does not have the user authorization method to log in:
conn.php
<?

Php
$conn = new Mongo ("Mongodb://user1:[email protected]:27017/test"); User Authorization link MongoDB test database
$db = $conn->test;
?>

find.php
<?php
Include "conn.php";
$c 1 = $db->c1; Operation C1 Collection

Because PHP can't use JSON directly
Db.c1.find ({name: "user1"}); Can't play like this.
{name: "user1"} = = Array ("name" = "user1") in this form
[up] = = Array (up);
{} = = Array ()

$arr =array ();
$rst = $c 1->find ($arr);
foreach ($rst as $val) {
echo "<pre>";
Print_r ($val [' name ']); "_ID" to take the ID.
}


Example 2: Specifying a value query
$arr = Array ("name" = "user1"); Query the Nam=user1
$rst = $c 1->find ($arr);
foreach ($rst as $val) {
echo "<pre>";
$fis = $val [' _id '];
Print_r ($val);
echo "<a href= ' user.php?"

fid={$fid} ' ></a> '; You will find that the FID has passed into the user.php when it becomes a string. How to solve?
user.php according to _id to search MongoDB corresponding data
<?php
Include "conn.php";
$c 1 = $db->c1;
$oid = new MongoId ($_get[' fid '); Use this to turn around.
Var_dump ($oid); or object, which is the string type.

$arr = Array ("_id" = "$oid");
$rst = $c 1->find ($arr);
foreach ($rst as $val) {
echo "<pre>";
Print_r ($val);
}
?>
}


Example 3: Adding
Include "conn.php";
$c 1 = $db->c1;
Db.c1.insert ({"Name" = "User3", age:30, "sex" = "Nan"});
$arr = Array ("name" = "User3", "Age" =>30, "sex" = "Nan");
if ($c 1->insert ($arr))
echo ' success ';
Else
echo ' failure ';




Example 4: Delete
Include "conn.php";
$c 1 = $db->c1;
Db.c1.remove ({"Name" = "User2"});
$arr = Array ("name" = "User2");
if ($c 1->remove ($arr))
Echo ' Delete succeeded ';
Else
Echo ' delete failed ';




Example 4: Change
Include "conn.php";
$c 1 = $db->c1;
Db.c1.update ({"Name" = "User2"},{$set: {age:20,sex: "Nan"}}); Add Field
$sarr = Array ("name" = "User2");
$darr = Array (' $set ' =>array (' sex ' = ' nan ', ' age ' =>24));
$opts = Array (' Upsert ' =>0, ' multiple ' =>1);
if ($c 1->update ($sarr, $darr, $opts)) The update in//php can only pass 3 of the parameters
Echo ' Change succeeded ';
Else
Echo ' Change failed ';

Shut down
$conn->close ();
?

>



PHP operation MongoDB Database specific sample introduction (increase, delete, change, check) (vi)

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.