PHP Connection MongoDB Sample Code _php Tutorial

Source: Internet
Author: User
Copy CodeThe code is as follows:
This is the default connection to the local 27017 port, of course you can also connect the remote host such as 192.168.0.4:27017, if the port is 27017, the port can be omitted
$m = new Mongo ();
Select the comedy database, if not previously the database will be automatically created, you can also use $m->selectdb ("comedy");
$db = $m->comedy;
Select the collection collection inside the comedy, which is equivalent to the table inside the RDBMS, and also-can be used
$collection = $db->collection;
$db->selectcollection ("collection");
Add an Element
$obj = Array ("title" = "Calvin and hobbes-". Date (' i:s '), "author" = "Bill Watterson");
Adding $obj to the $collection collection
$collection->insert ($obj);
Add another element
$obj = Array ("title" = "xkcd-". Date (' i:s '), "online" = true);
$collection->insert ($obj);
Querying all the records
$cursor = $collection->find ();
Traverse a document in all collections
foreach ($cursor as $obj)
{
echo $obj ["title"]. "
\ n ";
}
Delete all data
$collection->remove ();
Remove name as HM
$collection->remove (Array (' name ' = ' HM '));
Disconnecting MongoDB connections
$m->close ();
?>

http://www.bkjia.com/PHPjc/325839.html www.bkjia.com true http://www.bkjia.com/PHPjc/325839.html techarticle Copy the code as follows: PHP//Here is the default connection to the local 27017 port, of course you can also connect remote host such as 192.168.0.4:27017, if the port is 27017, the port can be omitted ...

  • 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.