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