$ 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 ");