Detailed usage process of MongoDB in PHP
$mongodb = new Mongo(); //$cOnnection= new Mongo( “$dburl:$port” ); // connect to a remote host (default port) $mydb = $mongodb->mydb; //Implicitly create database mydb $mydb = $mongodb->selectDB(“mydb”); //Select an existing database directly $collection = $mydb->mycollect; //Select the collection to be used. If it does not exist, it will be created automatically. $collection = $db->selectCollection(‘mydb’); //Only select, do not create //Insert new record $collection->insert(array(“name”=>”l4yn3”, “age”=>”10”, “sex”:”unknow”)); //Modify records $where = array(“name”=>”l4yn3”); $update_item = array(‘$set’=>array(“age”=>”15”, “sex”:”secret”)); $collection->update($where, $update_item); $options[‘multiple’] = true; //The default is false, whether to change the matching multiple lines $collection->update($where, $update_item, $options); //Query the records $myinfo= $collection->findOne(array(“name”=>”l4yn3”)); $myinfo = $collection->findOne(array(“name”=> “l4yn3”), array(“age”=>”15”)); //Search by condition: $query = array(“name”=>”l4yn3”); $cursor = $collection->find($query); //Find documents that satisfy $query in the $collectio collection while($cursor->hasNext()) { var_dump($cursor->getNext()); //Returned array } //Return the number of document records $collection->count(); //Delete a database: $connection->dropDB(“…”); //List all available databases: $m->listDBs(); //No return value //Close the connection: $connection->close(); PHP various parameter methods for connecting to mongodb database //Connect localhost:27017 $cOnn= new Mongo(); //Connect to the default port of the remote host $cOnn= new Mongo(‘test.com’); //Connect to the remote host port 22011 $cOnn= new Mongo(‘test.com:22011’); //MongoDB has username and password $cOnn= new Mongo(“mongodb://${username}:${password}@localhost”) //MongoDB…
Detailed usage process of MongoDB in PHP
$mongodb = new Mongo(); //$cOnnection= new Mongo( “$dburl:$port” ); // connect to a remote host (default port) $mydb = $mongodb->mydb; // implicitly create database mydb $mydb = $mongodb->selectDB(“mydb”); //Directly select the existing database $collection = $mydb->mycollect; //Select the collection used, if it does not exist, it will be created automatically $collection = $db->selectCollection(‘mydb’); // only select, not create //insert a new record $collection->insert(array(“name”=>”l4yn3”, “age”=>”10”, “sex”:”unknown”)); the //Modify record $where = array(“name”=>”l4yn3”); $update_item = array(‘$set’=>array(“age”=>”15”, “sex”:”secret”)); $collection->update($where, $update_item); $options[‘multiple’] = true; //The default is false, whether to change the matching multiple lines $collection->update($where, $update_item, $options); the //Query the records $myinfo= $collection->findOne(array(“name”=>”l4yn3”)); $myinfo = $collection->findOne(array(“name”=> “l4yn3”), array(“age”=>”15”)); the //Search by condition: $query = array(“name”=>”l4yn3”); $cursor = $collection->find($query); //Find documents satisfying $query in the $collectio collection while($cursor->hasNext()) { var_dump($cursor->getNext()); // returns the array } the //Return the number of document records $collection->count(); the //Delete a database: $connection->dropDB(“…”); //List all available databases: $m->listDBs(); //no return value //Close the connection: $connection->close(); Various parameter methods for connecting to mongodb database in php //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:22011’); //MongoDB has username and password…