PHP Operation MongoDB Database Detailed example introduction (increase, delete, change, check) (vi)

Source: Internet
Author: User

PHP Operation MongoDB:

PHP needs to play modules to manipulate MongoDB
Official website can be downloaded: Http://pecl.php.net/package/mongo download

MongoDB is set to user-authorized startup mode

The 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

Since PHP cannot be used directly in JSON
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 passed to user.php when it became a string, how to solve?
user.php data for MongoDB according to _ID
<?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: Increase
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: Deleting
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 parameters
Echo ' Change succeeded ';
Else
Echo ' Change failed ';

Shut down
$conn->close ();
?>



PHP Operation MongoDB Database Detailed example 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.