Download the corresponding php extension file php_mongo.dll, copy it into the php extension folder /php/ext, modify the php.ini file, and add the following lines:
extension=php_mongo.dll
PHP operation
Connect to database
$conn = new Mongo("mongodb://localhost:27017//admin:admin");
Select database and collection
Copy code
//Select database blog, if not, create it
$db = $conn->yyd;
//It can also be written as: $db = $conn->selectDB('yyd');
//Specify the result set (set: yyd_test)
$collection = $db->yyd_test;
//It can also be written as: $collection = $db->selectCollection('yyd_test');
//var_dump($collection);
Copy code
New data
$post = array('name' => '22', 'sex' => '32');
$flag=($collection->insert($post));
var_dump($flag);
image
image
Find data
Copy code
$arr=array();
$cursor = $collection->find($arr);
foreach($cursor as $key => $value){
echo "
";<div>
</div> echo $value['_id'];<div>
</div> echo '<br>name:';<div>
</div> echo $value['name'];<div>
</div> echo "<br>sex:";<div>
</div> echo $value['sex'];<div>
</div> echo "
";
}
Copy code
image
Condition search
$arr=array("name"=>"22");
$cursor = $collection->find($arr);
Modify data
image
Copy code
$newdata = array('$set' => array("email" => "test@test.com"));
$collection->update(array("name" => "22"), $newdata);
var_dump($collection);
$arr=array("name"=>"22");
$cursor = $collection->find($arr);
foreach($cursor as $key => $value){
echo "
";<div>
</div> echo $value['_id'];<div>
</div> echo '<br>name:';<div>
</div> echo $value['name'];<div>
</div> echo "<br>sex:";<div>
</div> echo $value['sex'];<div>
</div> echo "
email:";
echo $value['email'];
echo "
";
}
Copy code
Delete data
image
$arr=array("name"=>"22");
$collection->remove($arr);
var_dump($collection);
image
Other common operations
Copy code
//Close the connection
$conn->close();
//Delete a database
$conn->dropDB("yyd");
//List all available databases
$dbs = $conn->listDBs();
http://www.bkjia.com/PHPjc/735870.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/735870.htmlTechArticleDownload the corresponding php extension file php_mongo.dll, copy it into the php extension folder/php/ext, and modify it php.ini file, add the following lines: extension=php_mongo.dll PHP operation connects to the database $co...