MongoDB data with PHP CRUD

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:

  1. <?php
  2. m $ = new new MongoClient (); // default host and port connections: mongodb: // localhost: 27017
  3. DB $ = $ m -> Test ; // Get the name "test" database
  4. ?>

Create Collection

Create a set of code fragment is as follows:

  1. <?php
  2. m $ = new new MongoClient (); // connector
  3. DB $ = $ m -> Test ; // Get the name "test" database
  4. $collection = $db->createCollection("mongo");
  5. echo "create a collection of success" ;
  6. ?>

Performing the above procedure, the output results are as follows:

  1. Creating a collection of success

Insert Document

Use insert in mongoDB in () method to insert the document:

Snippet into the document as follows:

  1. <?php
  2. m $ = new new MongoClient (); // connected to mongodb
  3. DB $ = $ m -> Test ; // select a database
  4. Collection $ = $ DB -> Mongo ; // set selection
  5. $document = array(
  6. "title" => "MongoDB",
  7. "description" => "database",
  8. "likes" => 100,
  9. "url" => "http://www.mongodb.org.cn/",
  10. "by" => "Mongodb Chinese net"
  11. );
  12. $collection->insert($document);
  13. echo "data insertion success" ;
  14. ?>

Performing the above procedure, the output results are as follows:

  1. Data inserted successfully

Then we look at the data in the mongo client

  1. db.mongo.find().pretty();

Export

  1. {
  2. "_id": ObjectId("57512b3a57c9150f178b4567"),
  3. "title" : "MongoDB",
  4. "description" : "database",
  5. "likes" : NumberLong(100),
  6. "url" : "http://www.mongodb.org.cn/",
  7. "by" : "Mongodb Chinese net"
  8. }

Finding Documentation

Use find () method to read the documents in the collection.

Reading the document using the following code fragment:

  1. <?php
  2. m $ = new new MongoClient (); // connected to mongodb
  3. DB $ = $ m -> Test ; // select a database
  4. Collection $ = $ DB -> runoob ; // set selection
  5. the Cursor $ = $ Collection -> the Find (); // iteration display the document title
  6. foreach ($cursor as $document) {
  7. echo $document["title"] . "\n";
  8. }
  9. ?>

Performing the above procedure, the output results are as follows:

  1. MongoDB

Update Documentation

Use update () method to update the document.

The following examples will update the document entitled 'Tutorial MongoDB', the following code snippet:

  1. <?php
  2. m $ = new new MongoClient (); // connected to mongodb
  3. DB $ = $ m -> Test ; // select a database
  4. Collection $ = $ DB -> runoob ; // set selection
  5. // update the document
  6. $collection->update(array("title"=>"MongoDB"), array('$set'=>array("title"=>"MongoDB 教程")));
  7. // display the updated document
  8. $cursor = $collection->find();
  9. // cycle through the document title
  10. foreach ($cursor as $document) {
  11. echo $document["title"] . "\n";
  12. }
  13. ?>

Performing the above procedure, the output results are as follows:

  1. MongoDB Tutorial

Then we look at the data in the mongo client is being updated

  1. db.runoob.find().pretty();

Export

  1. {
  2. "_id": ObjectId("57512b3a57c9150f178b4567"),
  3. "title" : "MongoDB教程",
  4. "description" : "database",
  5. "likes" : NumberLong(100),
  6. "url" : "http://www.mongodb.org.cn/",
  7. "by" : "Mongodb Chinese net"
  8. }

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:

  1. <?php
  2. m $ = new new MongoClient (); // connected to mongodb
  3. DB $ = $ m -> Test ; // select a database
  4. Collection $ = $ DB -> Mongo ; // set selection
  5. // remove documents
  6. $collection->remove(array("title"=>"MongoDB 教程"), array("justOne" => true));
  7. // display the available document data
  8. $cursor = $collection->find();
  9. foreach ($cursor as $document) {
  10. echo $document["title"] . "\n";
  11. }
  12. ?>

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 .

Guess you like

Origin www.cnblogs.com/phpk/p/10939310.html