You can use MongoDB to develop applications using the logical structure:
● The MongoDB document is equivalent to a line of record in a relational database.
● Multiple documents form a set, which is equivalent to tables in a relational database.
● The logical combination of multiple sets is the database.
After installing and configuring MongoDB, we began to use PHP to perform basic database operations. The so-called basic operations are what we often call addition, deletion, modification, and query (CRUD.
I. PHP extension configuration
By default, PHP does not include mongodb Extensions. You can download the mongodb extension libraries from different regions to the corresponding libraries at http://www.php.net/manual/en/developer.installation.php.
My PHP version: 5.3.1 VC6 compiled version
Download the corresponding php extension file php_mongo.dll, and paste it into the php Extension folder/php/ext. Modify the php. Ini file and add the following lines:
Extension = php_cmd.dll
2. php crud tour
1. Connect to the database
The default username and password for mongodb are admin.
Set the username and password required for the connection and establish the database connection.
<? Php
$ Conn = new Mongo ("mongodb: // localhost: 27017 // admin: admin ");
// The default user and password are admin.
// Select the database blog. If no, create
$ Db = $ conn-> blog;
// You can also write $ db = $ conn-> selectDB ('blog ');
// Specify the result set (table name: posts)
$ Collection = $ db-> posts;
// You can also write it as $ collection = $ db-> selectCollection ('posts ');
?>
In this way, the blog database and the new dataset posts are successfully created (equivalent to the concept of a data table in SQL)
2. Create)
// Add a new blog posts
$ Post = array ('title' => 'first blog ', 'content' =>' this is the content of the first blog .');
$ Collection-> insert ($ post );
After successful addition, the data content is:
Array (3) {["_ id"] => object (Upload id) #8 (1) {["$ id"] => string (24) "4ee0799de1fecdbc16000001 & Prime;} [" title "] => string (15)" First blog "[" content "] => string (31) "This is the content of the first blog. "}
3. Query data (Read)
// Search
$ Cursor = $ collection-> find ();
Foreach ($ cursor as $ key => $ value ){
Var_dump ($ value );
}
4. Modify data (Update)
$ Newdata = array ('$ set' => array ("content" => "This is the content of the first updated blog ."));
$ Collection-> update (array ("title" => "first Blog"), $ newdata );
The updated data is as follows:
Array (3) {["_ id"] => object (Upload id) #7 (1) {["$ id"] => string (24) "4ee0799de1fecdbc16000001 & Prime;} [" content "] => string (43)" This is the content of the first updated blog. "[" title "] => string (15)" First blog "}
5. Delete data)
// Delete
$ Collection-> remove (array ('title' => 'first blog '));
6. Other common operations
// Close the connection
$ Conn-> close ();
// Delete a database
$ Conn-> dropDB ("blog ");
// List all available databases
$ Dbs = $ conn-> listDBs ();
Haha, the basic CRUD operation is complete. If you are interested, you can find other related php-mongodb API usage on the Internet.