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 ();
,