Home > Backend Development > PHP Tutorial > Summary of common operations of MongoDB in PHP_PHP tutorial

Summary of common operations of MongoDB in PHP_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-13 10:24:56
Original
824 people have browsed it

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


//Query records

$myinfo = $collection->findOne(array("name"=>"l4yn3"));

$myinfo = $collection->findOne(array("name"=>
"l4yn3"), array("age"=>"15"));


//Find by conditions:
$query = array("name"=>"l4yn3");
$cursor = $collection->find($query); // Find documents that satisfy $query in the $collectio collection
while($cursor->hasNext())
{
var_dump($cursor->getNext()); //The array is returned
}


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

Various parameter methods for php to connect to 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 the remote host port 22011
$conn = new Mongo('test.com:22011');
//MongoDB has a username 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");

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/825286.htmlTechArticle$mongodb = new Mongo(); //$connection = new Mongo( "$dburl:$port" ) ; // connect to a remote host (default port) $mydb = $mongodb-mydb; //Implicitly create database mydb $mydb = $mongodb-...
Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
mongodb start
From 1970-01-01 08:00:00
0
0
0
linux - ubuntu14 error installing mongodb
From 1970-01-01 08:00:00
0
0
0
Use of symfony2 mongodb
From 1970-01-01 08:00:00
0
0
0
mongodb _id rename
From 1970-01-01 08:00:00
0
0
0
Parameter understanding of mongodb
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template