This article is mainly about the PHP in addition to adding additions and deletions to change some of the other operations.
In the PHP operation MongoDB in the increase and deletion of the article we introduced the addition of MongoDB in PHP, delete, modify and query data operation. This article is mainly to use the query when the advanced knowledge to share with you.
1, the query when the sort
In relational database queries, sorting is often used. such as the reverse of time, click Rate Ascending, and so on. In the MongoDB query, there is also the sort function.
Syntax format:
$db->find ()->sort (Array (' age ' = 1); Sort by the age word orderby order. 1 is ascending,-1 is descending.
For example:
<?PHP//connecting to a database $connnect=NewMongo ("mongodb://127.0.0.1:27017"); //Select Database $db=$connnect->selectdb (' MyDB ')->selectcollection ("user")); //query all data, sorted by age word orderby order $cursor=$db->find ()Sort(Array(' Age ' = 1)); $array=Array(); while($cursor-Hasnext ()) { $array[] =$cursor-GetNext (); } Echo"<pre>"; Print_r($array);?>
The result of the operation is:
2. Skip and limit operations at query time
Similarly, in a relational database, limit can be used to restrict the query interval. There are similar features in MongoDB, which is the skip and limit operations. Skip is how many data is skipped before the limit is the number of data to query. For example, we'll start with the second piece of data and query for a piece of 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")); //according to the age word OrderBy order query, starting from the second, query a piece of data $cursor=$db->find ()Sort(Array(' age ' = 1)) ->skip (1)->limit (1); $array=Array(); while($cursor-Hasnext ()) { $array[] =$cursor-GetNext (); } Echo"<pre>"; Print_r($array);?>
The results of the operation are as follows:
3. Filter fields at query time
When querying, we often encounter the need to query only one or more specific fields, which involves field filtering. The syntax format is as follows:
$db->find (Array (), Array (' age ' = true); The first parameter is a query condition, and the second parameter is a field filter. A value of TRUE indicates that the field is queried, and a value of false indicates that the field is not queried.
For example above, we just want to query the age of this field, 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")); //query age and ID two fields. $cursor=$db->find (Array(),Array(' Age ' =true, ' id ' =true)); $array=Array(); while($cursor-Hasnext ()) { $array[] =$cursor-GetNext (); } Echo"<pre>";?>
The results of the operation are as follows:
4. Like operation when querying
When querying, we also encounter the need to blur the query. In a relational database, you can use a like query, and in a MongoDB database, there are also operations for fuzzy queries. The syntax format is as follows:
$db->find (arraynew Mongoregex ("/^j/"));
the fuzzy query in//MONGODB is through regular queries.
For example, the above example, we query the name of the data beginning with J, 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")); //querying data with the Name field starting with J $cursor=$db->find (Array(' name ' = =NewMongoregex ("/^j/"))); $array=Array(); while($cursor-Hasnext ()) { $array[] =$cursor-GetNext (); } Echo"<pre>"; Print_r($array);?>
The results of the operation are as follows:
5. Count operation at query time
When querying, we may encounter the sum of the data that needs to be queried to satisfy the criteria. The count operation is used. The syntax format is as follows:
$db->count ();
For example, in the example above, we query the sum of the data in the data database. 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"); Echo $dbCount();? >
Operation result is: 3
6, or operation of the query
When querying, we may also use the or operation. The syntax format is as follows:
$db->find (Array (' $or ' = = Array (' id ' = ' 1 '), Array (' name ' = ' java ')));
For example above, our query ID is 1, or name is Java 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")); $cursor=$db->find (Array(' $or ' =Array(Array(' id ' = 1),Array(' name ' = ' java ')))); $array=Array(); while($cursor-Hasnext ()) { $array[] =$cursor-GetNext (); } Echo"<pre>";?>
The operating result is:
PHP Operation MongoDB Advanced Query Chapter