Recently I wanted to make a basic method for front-end control interface field return, and specify the query fields through mongodb’s find($query,$field) query, but I encountered such a problem:
There are two encapsulation methods in the working code:
/**
* Query a record
* @param string collection name
* @param array query conditions
* @param array fields to be returned
* @return array query The result
*/
public function find_one($coll_name, $cOndition= array(), $ret_fields = array(‘_id’ => 0)) {
return $this->docsdb->find_one ($coll_name, $condition, $ret_fields);
}
/**
* Query
* @param string collection name
* @param array query condition array
such as array(col_a=>111)
* @param array collection filter array
The complete look is like this: array(sort=>array(col_a=>1,col_b=>-1), skip=>100, limit= >10, timeout=>5000, immortal=>true), which means:
*
- *
- wrapped uses wrapped as the key to encapsulate the array, and the default is in the order of the array.
- _id, by default no data will be returned with the MongoID field. If the return column is specified, the same effect will be achieved
- sort uses col_a as ASC, col_b For DESC sorting, you can combine multiple columns
- skip means starting from the 101st record to fetch data, that is, skipping the first 100 records
- limit this time The number of selected items
- timeout represents the time to wait for a response (not used yet)
- immortal represents whether to maintain the link permanently. The default is true. At this time timeout is invalid (not used yet)
*
*
*
*
*
*
*
* @param array The fields that need to be returned (usually only returning the necessary fields can speed up the response)
* @return mixed query The result
*/
public function find($coll_name, $cOndition= array(), $result_filter = array(‘wrapped’ => ”, ‘with_objectId’ => 0, ‘timeout’ => 5000 , ‘immortal’ => true), $ret_fields = array()) {
return $this->docsdb->find($coll_name, $condition, $result_filter, $ret_fields);
}
**
‘with_objectId‘ => 0 has been set to 1 when passing in parameters
**
According to the above description, $ret_fields is the field in mongodb find($query,$field), but through testing, I discovered such a problem. If I want to display the id, pass in Parameter array
$ret_fields = array(
’_id’ => 1,
‘account’=> 1,
‘card’ => 1
),
and call the find method, According to theory, the ‘_id’ field is also displayed at this time, and I directly query mongodb through this condition, and it is also displayed, but the printed data does not have an id. The data is as follows:
{
“account”: “qwe”,
“card”: “”
}
***Additional test results, if the find method does not pass in the $ret_fields variable, that is, it does not limit the returned fields, there will be ‘_id’
By modifying the conditions, I found that ‘_id‘ => 1. It doesn’t matter whether I write this condition or not, so I used the same one
$ret_fields called the find_one method, and at this time it actually returned the ‘_id’ field. The data is as follows
{
“_id”: {
“$id”: “58f472672430e11817000152”
},
“account”: “qwe”,
“card”: “”
}
Let me record this problem here first. If it is solved later, I will record the solution. If anyone can explain the reason, please feel free to give me some advice. Thank you!
About the problem of restricting the fields returned by php query mongodb