$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
//Connection localhost:27017
$conn = new Mongo ();
Connect to the remote host default port
$conn = new Mongo (' test.com ');
Connect to the remote host 22011 port
$conn = new Mongo (' test.com:22011 ');
MongoDB has user name password
$conn = new Mongo ("Mongodb://${username}:${password} @localhost")
//mongodb username password and specify database blog
$conn = new Mongo ("Mongodb://${username}:${password} @localhost/blog");
//Multiple servers
$conn = new Mongo ("mongodb://localhost:27017,localhost:27018");