1. Relational operators
$eq is equal to
$lt is less than
$lte is less than or equal to
$gt is greater than
$gte is greater than or equal to
$in is in scope
// Query the names of "Xiao Ming" and "Xiao Gang" $condition = [ 'name' => ['$in' => ['Xiao Ming', 'Xiao Gang' ]] ]; $result = $collection -> find($condition,['_id'=>false]);
2. Logical Operators
$and and (the default logical relationship is and)
// Query name whose age is greater than 10 and less than 30 $condition = [ '$and' => [ ['age' => ['$gt' => 10]], ['age' => ['$lt' => 30]] ] ]; $result = $collection -> find($condition,['_id'=>false]);
Shorthand for $and :
// Query name whose age is greater than 10 and less than 30 $condition = [ 'age' => ['$gt' => 10,'$lt' => 30 ] ]; $result = $collection -> find($condition,['_id'=>false]);
$or or
// Query name with age equal to 10 or equal to 30 $condition = [ '$or' => [ ['age' => ['$eq' => 10]], ['age' => ['$eq' => 30]] ] ]; $result = $collection -> find($condition,['_id'=>false]);
$not not
// Query name age is not equal to 10 $condition = [ 'age' => ['$not' => ['$eq' => 10 ]] ]; $result = $collection -> find($condition,['_id'=>false]);
3. Array condition
- When the value of the property is an array, the query needs special handling
- In the query condition, the value of the attribute is a string, which means that the search condition contains the string
// Query hobby containing swim $condition = [ 'hobby' => 'swim' ]; $result = $collection -> find($condition,['_id'=>false]);
-
- In the query condition, the value of the attribute contains multiple contents, which are connected using $all
// Query $condition in hobby that contains swim and climate = [ 'hobby' => ['$all' => ['swim','climb' ]] ]; $result = $collection -> find($condition,['_id'=>false]);
-
- In the query condition, the value of the attribute is an array, indicating that it is strictly equal to the element of the array
// The query hobby is the $condition of swim and climate = [ 'hobby' => ['swim','climb' ] ]; $result = $collection -> find($condition,['_id'=>false]);
4. Embedded Documentation
The embedded document behaves like the value of the property is again a document
For example: {
"name":"小明",
"score":{
"Math":100,
"Chinese”:90
}
}
- Get the property value of the embedded document through .dot
// Query math scores greater than 80 $condition = [ 'score.math' => ['$gt' => 80 ] ]; $result = $collection -> find($condition,['_id'=>false]);
5. Regular filtering
filter using regular expressions
// Query names starting with "small" $condition = [ 'name' => new MongoRegex('/^small/' ) ]; $result = $collection -> find($condition,['_id'=>false]);
6. Sort
Note: You need to query first, and then sort the results of the query
Sort by the sort method of the query result ( cursor ) , there are two ways to sort:
-
- 1 positive sequence
- -1 reverse order
// Query results $cursor = $collection -> find([],['_id'=> false ]); // Sort the query results in reverse order $result = $cursor -> sort (['score.math'=> -1 ]); foreach ( $result as $key => $value ){ echo ( $value ['name'].':'. $value ['score']['math']).'<br/ >' ; }
7. Limited quantity
Limit the results of the query, this method belongs to the cursor cursor
-
- how much skip() ignores
- limit() how much to keep
// Query results $cursor = $collection -> find([],['_id'=> false ]); // Sort the query results in reverse order, ignore the first three, and keep the last two $result = $cursor -> sort (['score.math'=>-1 ]) -> skip(3 ) -> limit(2 ); foreach ( $result as $key => $value ){ echo ( $value ['name']. ':'. $value ['score']['math']).'<br/>' ; }