1. Shell Connection using MongoDB:
Start Mongodb:mongod--dbpath d:\mongoDB\db
Open new shell, connect Mongodb:mongo
2. Shell Basic Command:
MongoDB command-line Operations
View all databases: Show DBS
Using a database: Use MyDB
View all collections: Show Collections
View all elements of a collection: Db.mycollection.find ()
Specify a value to view the corresponding element: Db.mycollection.find ({name: "Wing Jay"},{name:1, Age:1, id:0})
Delete database: Db.dropdatabase ()
View Current database: DB
View all current Collection:show collections
New collection collection:db.createCollection ("student") {"name": "Xiaoming"} or Db.student.insert ({"Name": "Xiaoming"})
Delete Collection Collection:db.student.drop ()
Rename Collection collection:db.student.renameCollection ("student") {"name": "Xiaoming"}
3. Use PHP code to perform MONGODB database operations
The first thing to do is to configure MongoDB in PHP, and then install the MongoDB database as well.
1. Connect to the database
The default initial user name and password for MongoDB are admin.
Establish the file mongotest.php, set the user name and password required for the connection, establish the database connection
<? php//Set UTF-8 encoding format
Header ("content-type:text/html; Charset=utf-8 "); $conn = new Mongo ("mongodb://localhost:27017//admin:admin" ); // Default user and password for admin//Select Database blog, if not, create /span> $db = $conn ->blog; // can also be written as: $db = $conn->selectdb (' Blog ‘); Set the result set (table name: posts) $collection = $db ->posts; // can also be written as: $collection = $db Selectcollection (' posts '); ?>
This successfully created the blog database and the new dataset posts (equivalent to the data table concept in SQL)
2. New Data (Create)
// added a blog posts $post Array (' title ' = ' The First blog ', ' content ' and ' = ' This is the contents of the first blog post. ') ); $collection->insert ($post);
After the new success, the data content is:
Array Object (MongoId) # 8 (1) {["$id"]=> string "4ee0799de1fecdbc16000001"} ["title"]=> string (15) "First blog" ["Content"]=> stri Ng (31) "This is the content of the first blog." }
3. Query data (Read)
// Find $cursor $collection, find (); foreach ($cursoras$key$value) { var_dump($value ); }
4. Updating data (update)
$newdata Array Array ("Content" = "This is the first blog post that was updated.") )); $collection->update (array$newdata);
The data content after the successful update is:
Array Object (MongoId) # 7 (1) {["$id"]=> string "4ee0799de1fecdbc16000001"} ["Content"]=> string (43) "This is the content of the first blog that was updated." ["title"]=> string (15) "First Blog"}
Batch Update
$content _1 Array (' title ' = ' The First blog ', ' content ' = ' happy Today '); $newdata Array (' $set ' + =array(' content ' = ' not only happy today, but also lucky to be very good hahaha ' )] $filter Array (' title ' = ' First blog ');//Filter $options true ;//batch update $collection->update ($filter,$newdata,$options);
5. Deleting data (delete)
// Delete $collection->remove (array(' title ' = ' First blog '));
6. Other common operation
// Close Connection $conn-Close (); // Delete a database $conn->dropdb ("blog"); // list all available databases $dbs $conn->listdbs ();
Use PHP to develop MongoDB and view the data in the shell