Summary of common operations of MongoDB in PHP

Source: Internet
Author: User
Tags findone

Summary of common operations of MongoDB in PHP

This article provides a detailed summary of common operations of MongoDB in PHP. If you need it, you can refer to it for help.

$ 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 an existing database

$ Collection = $ mydb-> mycollect; // select the selected collection. If the collection does not exist, it is automatically created.

$ Collection = $ db-> selectCollection ('mydb'); // select only, do not create

// Insert a new record

$ Collection-> insert (array ("name" => "l4yn3", "age" => "10", "sex": "unknow "));

// Modify the record

$ Where = array ("name" => "l4yn3 ");

$ Update_item = array ('$ set' => array ("age" => "15", "sex": "secret "));

$ Collection-> update ($ where, $ update_item );

$ Options ['Multiple '] = true; // The default value is false. Whether to change the matching multiple rows

$ Collection-> update ($ where, $ update_item, $ options );

// Query records

$ Myinfo = $ collection-> findOne (array ("name" => "l4yn3 "));

$ Myinfo = $ collection-> findOne (array ("name" =>

"L4yn3"), array ("age" => "15 "));

// Search by conditions:

$ Query = array ("name" => "l4yn3 ");

$ Cursor = $ collection-> find ($ query); // search for documents that meet $ query in the $ collectio collection

While ($ cursor-> hasNext ())

{

Var_dump ($ cursor-> getNext (); // an array is returned.

}

// Number of returned document records

$ Collection-> count ();

// Delete a database:

$ Connection-> dropDB ("...");

// List all available databases:

$ M-> listDBs (); // No Return Value

// Close the connection:

$ Connection-> close ();

Various php parameter methods for connecting to the mongodb Database

// Connect to localhost: 27017

$ Conn = new Mongo ();

// Connect to the default port of the remote host

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

// Connect to port 22011 of the remote host

$ Conn = new Mongo ('test. com: 100 ');

// MongoDB has a user name and password

$ Conn = new Mongo ("mongodb: // $ {username }:: {password} @ localhost ")

// MongoDB has a username and password and specifies the Database blog

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

// Multiple servers

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

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.