Summary of common operations of MongoDB in PHP

Source: Internet
Author: User
Tags findone mongodb

This article is mainly to MongoDB in PHP in the common operation of a detailed summary of the introduction, the need for friends can come to the reference, I hope to help you

$mongodb = new Mongo ();

$connection = new Mongo ("$dburl: $port"); Connect to a remote host (default port)

$mydb = $mongodb->mydb; Implicitly create a database mydb

$mydb = $mongodb->selectdb ("MyDB"); Directly select a database that already exists

$collection = $mydb->mycollect; Select the collected anthology, if not present, automatically create

$collection = $db->selectcollection (' mydb '); Select only, do not create

Insert new record

$collection->insert (Array ("name" => "L4yn3", "Age" => "ten", "Sex": "Unknow"));

Modify Records

$where = Array ("name" => "L4yn3");

$update _item = Array (' $set ' =>array ("Age" => ", Sex": "secret"));

$collection->update ($where, $update _item);

$options [' multiple '] = true; Default is False, do you want to change a matching multiline

$collection->update ($where, $update _item, $options);

Query records

$myinfo = $collection->findone (Array ("name" => "L4yn3"));

$myinfo = $collection->findone (Array ("name" =>

"L4yn3"), Array ("Age" => "15"));

Find by Condition:

$query = Array ("name" => "L4yn3");

$cursor = $collection->find ($query); Find documents that meet $query in the $collectio collection

while ($cursor->hasnext ())

{

Var_dump ($cursor->getnext ()); Returned the array

}

Returns the number of document records

$collection->count ();

To delete a database:

$connection->dropdb ("...");

List all available databases:

$m->listdbs (); No return value

Close connection:

$connection->close ();

PHP various connection MongoDB database parameter way

Connect localhost:27017

$conn = new Mongo ();

Connecting to the remote host default port

$conn = new Mongo (' test.com ');

Connecting to remote host port 22011

$conn = new Mongo (' test.com:22011 ');

MongoDB has user name password

$conn = new Mongo ("Mongodb://${username}:${password} @localhost")

MongoDB has a username password and specifies a database blog

$conn = new Mongo ("Mongodb://${username}:${password} @localhost/blog");

Multiple servers

$conn = new Mongo ("mongodb://localhost:27017,localhost:27018");

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.