There is a project using the MongoDB database, query conditions have and also, according to thinkphp official manual, using a composite query (_complex), Getlastsql output query statement, found that the query condition is empty. Query with string mode (_string), The request string query (_query) does not meet the requirements. It is estimated that the number of MongoDB users is low and thinkphp official support for this is insufficient. Open the MongoDB drive of thinkphp, thinkphp/extend/driver/db/ DbMongo.class.php, find protected function Parsethinkwhere ($key, $val) method, you can find that there is no _complex in switch, that is to say, When thinkphp uses MongoDB, it does not support composite queries at all. Plus:
Copy Code code as follows:
Case ' _complex '://Compound Query
$arr = Array ();
foreach ($val as $nkey => $nval) {
if (Strpos ($nkey, ' _ ')!=0)
{
$PARSEARR = $this->parsewhereitem ($nkey, $nval);
Convert to Object
$obj =new StdClass ();
foreach ($parseArr as $pkey => $pval)
{
$obj-> $pkey = $pval;
}
Array_push ($arr, $obj);
}
}
if (Isset ($val [' _logic ']) && strtolower ($val [' _logic ']) = = ' or ') {
unset ($val [' _logic ']);
$query [' $or '] = $arr;
}
Break
The reason to convert to objects is because using thinkphp uses the Json_encode function to generate the query statement, but if the array element takes the Key,json_encode function to convert the array into an object form, the MongoDB is not recognized. Because you currently use only or, so , the code only handles the OR.
Also, find a bug (not counted) in the Parsewhere method:
Copy Code code as follows:
foreach ($where as $key => $val) {
if (' _id '!= $key && 0===strpos ($key, ' _ ')) {
Parsing Special Conditional expressions
The original $query = $this->parsethinkwhere ($key, $val);
$query = Array_merge ($query, $this->parsethinkwhere ($key, $val));
}else{
Security filtering for query fields
if (!preg_match ('/^[a-z_\|\&\-.a-z0-9]+$/', trim ($key))) {
Throw_exception (L (' _error_query_ '). ': '. $key);
}
$key = Trim ($key);
if (Strpos ($key, ' | ')) {
$array = Explode (' | ', $key);
$str = Array ();
foreach ($array as $k) {
$str [] = $this->parsewhereitem ($k, $val);
}
$query [' $or '] = $str;
}elseif (Strpos ($key, ' & ')) {
$array = Explode (' & ', $key);
$str = Array ();
foreach ($array as $k) {
$str [] = $this->parsewhereitem ($k, $val);
}
$query = Array_merge ($query, $STR);
}else{
$str = $this->parsewhereitem ($key, $val);
$query = Array_merge ($query, $STR);
}
}
}
When parsing a special conditional expression, the source code is $query= $this->parsethinkwhere ($key, $val), and an error occurs when the special expression is not the first element in the where array, and the $query array in the else's code. All gone.