SoFunction
Updated on 2025-03-09

Introduction to PHP and MongoDB | Security | Detailed explanation of M+PHP application examples

1. Introduction to MongoDB
MongoDB (name from "humongous") is a scalable, high-performance, open source, free-schema, document-oriented database that combines the advantages of document databases, key-value pairs of storage and relational databases. Official site: /, MongoDB features:

• Document storage (JSON-like data mode is simple and powerful)
• Dynamic query
• Full index support, extended to internal objects and embedded arrays
•Query record analysis
•Fast, on-site updates
•Efficient storage of large binary objects (such as photos and videos)
• Replication and failover support
•Auto-Sharding automatic sharding supports cloud-level scaling
•MapReduce supports complex aggregation
•Business support, training and consultation

2. Install MongoDB
Installing MongoDB is very simple. You only need to download the compressed package to decompress and run the command. The download address is: /downloads. This article is a Windows platform. MongoDB run the command: >bin/mongod. Tip: First, you need to create a folder that stores data. MongoDB's default storage data directory is /data/db/ (or c:\data\db). Of course, you can also modify it to a different directory. You only need to specify the --dbpath parameter, eg:
>bin/mongod --dbpath=d:\mgdata\db

3. Install MongoDB PHP extension
Download the PHP extension according to your own PHP version: /mongodb/mongo-php-driver/downloads, prompt:
1. VC6 is suitable for Apache, VC9 is suitable for IIS;
2. Thread safe is suitable for PHP to operate in modules, and Non-thread safe is suitable for CGI operation.
Modify, add: extension=php_mongo.dll, and restart the web server.

4. Related examples of PHP testing and MongoDB
1. Connect to Mongo server

Copy the codeThe code is as follows:

<?php
//Connect localhost:27017
$conn = new Mongo();
//Connect the default port of the remote host
$conn = new Mongo('');
//Connect the remote host port 22011
$conn = new Mongo(':22011');
//MongoDB has 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");
?>

Copy the codeThe code is as follows:

<?php
//Connect localhost:27017
$conn = new Mongo();
//Connect the default port of the remote host
$conn = new Mongo('');
//Connect the remote host port 22011
$conn = new Mongo(':22011');
//MongoDB has 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");
?>

2. Specify the database and dataset name (table name)
Copy the codeThe code is as follows:

<?php
//Select database blog
$db = $conn->blog;
//Develop the result set (table name: users)
$collection = $db->users;
?>

Copy the codeThe code is as follows:

<?php
//Select database blog
$db = $conn->blog;
//Develop the result set (table name: users)
$collection = $db->users;
?>

3、CRUD
Copy the codeThe code is as follows:

<?php
//Add new
$user = array('name' => 'caleng', 'email' => 'admin@');
$collection->insert($user);
//Revise
$newdata = array('$set' => array("email" => "test@"));
$collection->update(array("name" => "caleng"), $newdata);
//delete
$collection->remove(array('name'=>'caleng'), array("justOne" => true));
//Find
$cursor = $collection->find();
var_dump($cursor);
//Find one
$user = $collection->findOne(array('name' => 'caleng'), array('email'));
var_dump($user);
?>

Copy the codeThe code is as follows:

<?php
//Add new
$user = array('name' => 'caleng', 'email' => 'admin@');
$collection->insert($user);
//Revise
$newdata = array('$set' => array("email" => "test@"));
$collection->update(array("name" => "caleng"), $newdata);
//delete
$collection->remove(array('name'=>'caleng'), array("justOne" => true));
//Find
$cursor = $collection->find();
var_dump($cursor);
//Find one
$user = $collection->findOne(array('name' => 'caleng'), array('email'));
var_dump($user);
?>

4. Close the connection
Copy the codeThe code is as follows:

<?php
$conn->close();
?>