Php basic tutorial on MongoDB operations (connection, addition, modification, deletion, and query)

Source: Internet
Author: User
This article describes a simple php tutorial on MongoDB operations, including connection, addition, modification, deletion, and query. For more information, see

This article describes a simple php tutorial on MongoDB operations, including connection, addition, modification, deletion, and query. For more information, see

The Code is as follows:


// 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 ");
// Select the Database blog
$ Db = $ conn-> blog;
// Specify the result set (Table Name: users)
$ Collection = $ db-> users;
// Add
$ User = array ('name' => 'caleng', 'email '=> 'admin # admin.com ');
$ Collection-> insert ($ user );
// Modify
$ Newdata = array ('$ set' => array ("email" => "test@test.com "));
$ Collection-> update (array ("name" => "caleng"), $ newdata );
// Delete
$ Collection-> remove (array ('name' => 'caleng'), array ("justOne" => true ));
// Search
$ Cursor = $ collection-> find ();
Var_dump ($ cursor );
// Find one
$ User = $ collection-> findOne (array ('name' => 'caleng'), array ('email '));
Var_dump ($ user );
// Close the database
$ Conn-> close ();


,

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.