PHP operation MongoDB to increase the deletion of search articles

Source: Internet
Author: User

Before I explained the installation of the MongoDB extension in PHP, and started, the link operation "forgot?" Go and see it! PHP operates one of MongoDB ". This article is mainly about the addition of MongoDB in PHP, query, modify and delete data operations.

1. Add Data

Syntax format:

$db->insert ($array); parameter represents the data that needs to be inserted

For example: We insert an ID of 1, the name is PHP, the age of 25 data. The code is as follows:

<?PHP//connecting to a database    $connnect=NewMongo ("mongodb://127.0.0.1:27017"); //Select Database    $db=$connnect->selectdb (' MyDB ')->selectcollection ("user")); //organize the data you need to insert    $array=Array(); $array[' id '] = 1; $array[' name '] = ' php '; $array[' age '] = 25; //Inserting Data    $db->insert ($array);?>

In this way, a piece of data is inserted into the user collection of MyDB. Of course, the above $db->insert ($array), can also be replaced with $db->save ($array);. The difference between insert () and save () is that if there is a primary key, insert () does not operate, and save () changes the original content to new content.

2. Query data

Syntax format:

$db->find (Array (' _id ' = = new MongoId ($id))); Parameter can be empty, then all data is queried

For example, we query all the data in the database with the following code:

<?PHP//connecting to a database    $connnect=NewMongo ("mongodb://127.0.0.1:27017"); //Select Database    $db=$connnect->selectdb (' MyDB ')->selectcollection ("user")); //Querying Data    $cursor=$db-find (); $array=Array();  while($cursor-Hasnext ()) {        $array[] =$cursor-GetNext (); }    Echo"<pre>"; Print_r($array);?>

The results of the operation are as follows:

The visible data has been removed. If you want to remove only one piece of data. You can use $db->findone (); At this point, only the data for the first document is taken out.

queries for specific conditions

For example, to query for information about a member older than 25, write the condition in Find (), with the following code:

<?PHP//connecting to a database    $connnect=NewMongo ("mongodb://127.0.0.1:27017"); //Select Database    $db=$connnect->selectdb (' MyDB ')->selectcollection ("user")); //Query Criteria    $cursor=$db->find (Array(' Age ' =Array(' $gt ' = 25))); $array=Array();  while($cursor-Hasnext ()) {        $array[] =$cursor-GetNext (); }?>

Note that ' $GT ' is a single quote!

3. Modify the data

Syntax format:

$db->update (Array (' _id ' = = new MongoId ($id)), $array); The first parameter is the specified condition, and the second parameter is the object to be updated

For example, we modify the data with ID 537097b59067916c06000003 as follows:

<?PHP//connecting to a database    $connnect=NewMongo ("mongodb://127.0.0.1:27017"); //Select Database    $db=$connnect->selectdb (' MyDB ')->selectcollection ("user")); //data that needs to be modified    $array=Array(); $array[' id '] = 1; $array[' name '] = ' java '; $array[' age '] = 25; $db->update (Array(' _id ' =NewMongoId (' 537097b59067916c06000003 ')),$array);?>

This modifies the data with ID 537097b59067916c06000003.

4. Delete data

Syntax format:

$db->remove (Array (' _id ' = = new MongoId ($id)), Array (' justone ' = True)); The first parameter is the specified condition, and if the Justone parameter is added, only one record that matches the condition is deleted, and the other does not delete

For example, we delete the data with ID 5370A05D4B628998570CDF6D, the code is as follows:

<? PHP     // connecting to a database    $connnect New Mongo ("mongodb://127.0.0.1:27017");     // Select Database    $db $connnect->selectdb (' MyDB ')->selectcollection ("User");     // Data that needs to be deleted    $db->remove (arraynew MongoId (' 5370a05d4b628998570cdf6d ')));? >

This removes the data with ID 5370a05d4b628998570cdf6d.

PHP operation MongoDB to increase the deletion of the search article

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.