Original Address: https://www.mongodb.org.cn/drivers/2.html (The site is mongoDB official website)
php use php mongodb you have to use the drive mongodb.
MongoDB PHP installed on each platform and download the driver package, please see: PHP install MongoDB expansion drive
If you are using PHP7, please see: PHP7 MongoDB installation and use .
Select a database and ensure that the connection
To ensure proper connection, you need to specify the database name, if the database does not exist in mongoDB, mongoDB automatically created
The following code snippet:
- <?php
- m $ = new new MongoClient (); // default host and port connections: mongodb: // localhost: 27017
- DB $ = $ m -> Test ; // Get the name "test" database
- ?>
Create Collection
Create a set of code fragment is as follows:
- <?php
- m $ = new new MongoClient (); // connector
- DB $ = $ m -> Test ; // Get the name "test" database
- $collection = $db->createCollection("mongo");
- echo "create a collection of success" ;
- ?>
Performing the above procedure, the output results are as follows:
- Creating a collection of success
Insert Document
Use insert in mongoDB in () method to insert the document:
Snippet into the document as follows:
- <?php
- m $ = new new MongoClient (); // connected to mongodb
- DB $ = $ m -> Test ; // select a database
- Collection $ = $ DB -> Mongo ; // set selection
- $document = array(
- "title" => "MongoDB",
- "description" => "database",
- "likes" => 100,
- "url" => "http://www.mongodb.org.cn/",
- "by" => "Mongodb Chinese net"
- );
- $collection->insert($document);
- echo "data insertion success" ;
- ?>
Performing the above procedure, the output results are as follows:
- Data inserted successfully
Then we look at the data in the mongo client
- db.mongo.find().pretty();
Export
- {
- "_id": ObjectId("57512b3a57c9150f178b4567"),
- "title" : "MongoDB",
- "description" : "database",
- "likes" : NumberLong(100),
- "url" : "http://www.mongodb.org.cn/",
- "by" : "Mongodb Chinese net"
- }
Finding Documentation
Use find () method to read the documents in the collection.
Reading the document using the following code fragment:
- <?php
- m $ = new new MongoClient (); // connected to mongodb
- DB $ = $ m -> Test ; // select a database
- Collection $ = $ DB -> runoob ; // set selection
- the Cursor $ = $ Collection -> the Find (); // iteration display the document title
- foreach ($cursor as $document) {
- echo $document["title"] . "\n";
- }
- ?>
Performing the above procedure, the output results are as follows:
- MongoDB
Update Documentation
Use update () method to update the document.
The following examples will update the document entitled 'Tutorial MongoDB', the following code snippet:
- <?php
- m $ = new new MongoClient (); // connected to mongodb
- DB $ = $ m -> Test ; // select a database
- Collection $ = $ DB -> runoob ; // set selection
- // update the document
- $collection->update(array("title"=>"MongoDB"), array('$set'=>array("title"=>"MongoDB 教程")));
- // display the updated document
- $cursor = $collection->find();
- // cycle through the document title
- foreach ($cursor as $document) {
- echo $document["title"] . "\n";
- }
- ?>
Performing the above procedure, the output results are as follows:
- MongoDB Tutorial
Then we look at the data in the mongo client is being updated
- db.runoob.find().pretty();
Export
- {
- "_id": ObjectId("57512b3a57c9150f178b4567"),
- "title" : "MongoDB教程",
- "description" : "database",
- "likes" : NumberLong(100),
- "url" : "http://www.mongodb.org.cn/",
- "by" : "Mongodb Chinese net"
- }
Delete Document
Use remove () method to delete the document.
The following examples we will remove the 'title' is 'MongoDB tutorial' of a data record. , The following code snippet:
- <?php
- m $ = new new MongoClient (); // connected to mongodb
- DB $ = $ m -> Test ; // select a database
- Collection $ = $ DB -> Mongo ; // set selection
- // remove documents
- $collection->remove(array("title"=>"MongoDB 教程"), array("justOne" => true));
- // display the available document data
- $cursor = $collection->find();
- foreach ($cursor as $document) {
- echo $document["title"] . "\n";
- }
- ?>
In addition to the above examples, in php you can use findOne (), save (), limit (), skip (), sort () method and the like to operate Mongodb database.
More method of operation reference may Mongodb core classes: http://php.net/manual/zh/mongo.core.php .