1024programmer Java Regarding the problem of restricting return fields in php query mongodb

Regarding the problem of restricting return fields in php query mongodb

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:


  1. wrapped uses wrapped as the key to encapsulate the array, and the default is in the order of the array.
  2. *

  3. _id, by default no data will be returned with the MongoID field. If the return column is specified, the same effect will be achieved
  4. *

  5. sort uses col_a as ASC, col_b For DESC sorting, you can combine multiple columns
  6. *

  7. skip means starting from the 101st record to fetch data, that is, skipping the first 100 records
  8. *

  9. limit this time The number of selected items
  10. *

  11. timeout represents the time to wait for a response (not used yet)
  12. *

  13. immortal represents whether to maintain the link permanently. The default is true. At this time timeout is invalid (not used yet)
  14. *

* @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

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/764817

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us


Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top